{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Demo Model for NCA Emoji Challenge","metadata":{}},{"cell_type":"markdown","source":"This is a demo for an NCA Model.  Let's see if a dinosaur can regenerate. We'll use a herbivore just in case. \n\nModels for the competition were trained on CPU so GPU is optional.","metadata":{}},{"cell_type":"code","source":"# notebook using NCA emoji challenge script for NCA model for growing NCAs","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref\n# https://distill.pub/2020/growing-ca/\n\n#https://colab.research.google.com/github/google-research/self-organising-systems/blob/master/notebooks/growing_ca.ipynb\n\n# Licensed under the Apache License, Version 2.0   https://www.apache.org/licenses/LICENSE-2.0\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to get script for NCA Emoji Challenge - \n# File -> Add utility script\n#   add to sys.path the ../usr/lib/<script name folder>/<script name.py>\n#   then can do import","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nSPATH = '../usr/lib/nca_emoji_challenge_script/nca_emoji_challenge_script.py'  \nsys.path.insert(1, SPATH)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:00:09.038564Z","iopub.execute_input":"2022-07-30T11:00:09.039058Z","iopub.status.idle":"2022-07-30T11:00:09.069071Z","shell.execute_reply.started":"2022-07-30T11:00:09.038961Z","shell.execute_reply":"2022-07-30T11:00:09.068168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from nca_emoji_challenge_script import *","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:00:11.798632Z","iopub.execute_input":"2022-07-30T11:00:11.799017Z","iopub.status.idle":"2022-07-30T11:00:18.984951Z","shell.execute_reply.started":"2022-07-30T11:00:11.798987Z","shell.execute_reply":"2022-07-30T11:00:18.983821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CAModel().dmodel.summary()  # model from script should appear ","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:00:33.637765Z","iopub.execute_input":"2022-07-30T11:00:33.638495Z","iopub.status.idle":"2022-07-30T11:00:34.644692Z","shell.execute_reply.started":"2022-07-30T11:00:33.638456Z","shell.execute_reply":"2022-07-30T11:00:34.643429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGET_EMOJI = '🦕' # the sauropod emoji\nTRAIN_ITERS = 7000  # default in script you can change sometimes 5000 is enough ","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:01:43.726977Z","iopub.execute_input":"2022-07-30T11:01:43.727987Z","iopub.status.idle":"2022-07-30T11:01:43.732910Z","shell.execute_reply.started":"2022-07-30T11:01:43.727945Z","shell.execute_reply":"2022-07-30T11:01:43.731746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ucode = get_codestring(TARGET_EMOJI)\nprint(ucode)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:02:19.860961Z","iopub.execute_input":"2022-07-30T11:02:19.861376Z","iopub.status.idle":"2022-07-30T11:02:19.866490Z","shell.execute_reply.started":"2022-07-30T11:02:19.861345Z","shell.execute_reply":"2022-07-30T11:02:19.865731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fname = ucode_to_filename(ucode,prefix,suffix)\nfpath = PATH+'/'+fname\nimg = PIL.Image.open(fpath)\nimshow((img),fmt='png')","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:04:45.861448Z","iopub.execute_input":"2022-07-30T11:04:45.861877Z","iopub.status.idle":"2022-07-30T11:04:45.890863Z","shell.execute_reply.started":"2022-07-30T11:04:45.861841Z","shell.execute_reply":"2022-07-30T11:04:45.889754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_size=TARGET_SIZE\nimg.thumbnail((max_size, max_size), PIL.Image.ANTIALIAS)\nimg = np.float32(img)/255.0    \n    # premultiply RGB by Alpha\nimg[..., :3] *= img[..., 3:]\n\ntarget_img = img\nimshow(zoom(to_rgb(target_img), 2), fmt='png')","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:07:51.151493Z","iopub.execute_input":"2022-07-30T11:07:51.151874Z","iopub.status.idle":"2022-07-30T11:07:51.177770Z","shell.execute_reply.started":"2022-07-30T11:07:51.151841Z","shell.execute_reply":"2022-07-30T11:07:51.176771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ## Initialize Training\n\np = TARGET_PADDING\npad_target = tf.pad(target_img, [(p, p), (p, p), (0, 0)])\nh, w = pad_target.shape[:2]\nseed = np.zeros([h, w, CHANNEL_N], np.float32)\nseed[h//2, w//2, 3:] = 1.0\n\ndef loss_f(x):\n    return tf.reduce_mean(tf.square(to_rgba(x)-pad_target), [-2, -3, -1])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:10:28.342547Z","iopub.execute_input":"2022-07-30T11:10:28.342934Z","iopub.status.idle":"2022-07-30T11:10:28.360412Z","shell.execute_reply.started":"2022-07-30T11:10:28.342902Z","shell.execute_reply":"2022-07-30T11:10:28.358947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ca = CAModel()\n\nloss_log = []\n\nlr = 3e-3  # 2e-3\nlr_sched = tf.keras.optimizers.schedules.PiecewiseConstantDecay(\n    [2000], [lr, lr*0.1])  # 2000\ntrainer = tf.keras.optimizers.Adam(lr_sched)\n\nloss0 = loss_f(seed).numpy()\npool = SamplePool(x=np.repeat(seed[None, ...], POOL_SIZE, 0))","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:10:33.951761Z","iopub.execute_input":"2022-07-30T11:10:33.952159Z","iopub.status.idle":"2022-07-30T11:10:34.281861Z","shell.execute_reply.started":"2022-07-30T11:10:33.952127Z","shell.execute_reply":"2022-07-30T11:10:34.280845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a folder in tmp for train_log output to zip later in case exceeds 500 files","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /tmp/train_log/","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_training(pool,loss_f,seed,loss_log,h,w,ca,trainer,TRAIN_ITERS)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"archn = 'nca_trainlog'\nshutil.make_archive(archn, 'zip', '/tmp/train_log')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = TRAIN_ITERS\nmodel_wts = '/tmp/train_log/%04d'%i \nprint(model_wts)\nca = CAModel()\nca.load_weights(model_wts)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ca.save_weights('kaggle/working/jurassic_nca')  ","metadata":{},"execution_count":null,"outputs":[]}]}