{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# https://github.com/visipedia/iwildcam_comp\n# https://github.com/microsoft/CameraTraps/blob/master/megadetector.md","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# !pip install gcsfs\nfrom glob import glob\nimport math, os, time, re, json, shutil, pprint, random, gc\nimport tensorflow as tf\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom kaggle_datasets import KaggleDatasets\n\nprint(\"Tensorflow version \" + tf.__version__)\nAUTO = tf.data.experimental.AUTOTUNE\n# \nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, MaxPooling2D, Activation, GlobalMaxPooling2D, concatenate\nfrom tensorflow.keras.layers import Conv2D, Dropout, BatchNormalization, Input, SeparableConv2D, Flatten\nfrom tensorflow.keras.layers import add as add_concat\nfrom tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import backend as K\n\nfrom IPython.display import clear_output\nimport IPython.display as display\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nnp.random.seed(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver() \n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy()\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# TRAIN_PATERN = KaggleDatasets().get_gcs_path('iwidcam-2020-train-tfrecords')\n# !gsutil ls $GCS_DS_PATH\nTRAIN_PATERN = '/kaggle/input/iwidcam-2020-train-tfrecords'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if strategy.num_replicas_in_sync == 1: # single GPU or CPU\n    BATCH_SIZE = 256\n    VALIDATION_BATCH_SIZE = 256\nelse: # TPU pod\n    BATCH_SIZE = 16 * strategy.num_replicas_in_sync\n    VALIDATION_BATCH_SIZE = 16 * strategy.num_replicas_in_sync\n    \nFILENAMES = tf.io.gfile.glob(TRAIN_PATERN+'/*.tfrec')\n\nIMAGE_SIZE = [64, 64]\nIMAGE_TARGET = [64, 64]\nif K.image_data_format() == 'channels_first':\n    SHAPE = (3,*IMAGE_SIZE)\n    INPUT_SHAPE = (3, *IMAGE_TARGET)\nelse:\n    SHAPE = (*IMAGE_SIZE, 3)\n    INPUT_SHAPE = (*IMAGE_TARGET, 3)\nSIZE_TFRECORD = 1024\nsplit = int(len(FILENAMES)*0.81)\nTRAINING_FILENAMES = FILENAMES[:split]\nVALIDATION_FILENAMES = FILENAMES[split:]\nSTEP_PER_EPOCH = (len(TRAINING_FILENAMES)*SIZE_TFRECORD)//BATCH_SIZE\nVALIDATION_STEP_PER_EPOCH = (len(VALIDATION_FILENAMES)*SIZE_TFRECORD)//VALIDATION_BATCH_SIZE\nprint(len(TRAINING_FILENAMES))\nprint(len(VALIDATION_FILENAMES))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def read_tfrecord(example):\n    features = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), \n        \"class\": tf.io.FixedLenFeature([], tf.int64), \n        \"iage_id\": tf.io.FixedLenFeature([], tf.string),\n        \"label\": tf.io.VarLenFeature(tf.float32) ,\n        \"size\": tf.io.FixedLenFeature([2], tf.int64) \n    }\n    example = tf.io.parse_single_example(example, features)\n    image = tf.image.decode_jpeg(example['image'], channels=3)\n\n    iage_id = example['iage_id']\n    class_num = example['class']\n    label = tf.sparse.to_dense(example['label'])\n\n    return image,label\n\noption_no_order = tf.data.Options()\noption_no_order.experimental_deterministic = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_9_images_from_dataset(dataset):\n    plt.figure(figsize=(13,13))\n    subplot=331\n    for i, (image, label) in enumerate(dataset):\n        plt.subplot(subplot)\n        plt.axis('off')\n        plt.imshow(image.numpy().astype(np.uint8))\n        plt.title(label.numpy().decode(\"utf-8\"), fontsize=16)\n        subplot += 1\n        if i==8:\n            break\n    plt.tight_layout()\n    plt.subplots_adjust(wspace=0.1, hspace=0.1)\n    plt.show()\ndef get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):\n    rotation = math.pi * rotation / 180.\n    shear = math.pi * shear / 180.\n    # ROTATION MATRIX\n    c1 = tf.math.cos(rotation)\n    s1 = tf.math.sin(rotation)\n    one = tf.constant([1],dtype='float32')\n    zero = tf.constant([0],dtype='float32')\n    rotation_matrix = tf.reshape( tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3] )\n    # SHEAR MATRIX\n    c2 = tf.math.cos(shear)\n    s2 = tf.math.sin(shear)\n    shear_matrix = tf.reshape( tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3] )    \n    # ZOOM MATRIX\n    zoom_matrix = tf.reshape( tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3] )\n    # SHIFT MATRIX\n    shift_matrix = tf.reshape( tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3] )\n    \n    return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))\n\n\ndef transform(image,label):\n    DIM = IMAGE_SIZE[0]\n    XDIM = DIM%2 #fix for size\n    \n    rot = 15. * tf.random.normal([1],dtype='float32')\n    shr = 5. * tf.random.normal([1],dtype='float32') \n    h_zoom = 0.8 + tf.random.normal([1],dtype='float32')/10.\n    w_zoom = 0.8 + tf.random.normal([1],dtype='float32')/10.\n    h_shift = 16. * tf.random.normal([1],dtype='float32') \n    w_shift = 16. * tf.random.normal([1],dtype='float32') \n    # GET TRANSFORMATION MATRIX\n    m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift) \n    # LIST DESTINATION PIXEL INDICES\n    x = tf.repeat( tf.range(DIM//2,-DIM//2,-1), DIM )\n    y = tf.tile( tf.range(-DIM//2,DIM//2),[DIM] )\n    z = tf.ones([DIM*DIM],dtype='int32')\n    idx = tf.stack( [x,y,z] )\n    # ROTATE DESTINATION PIXELS ONTO ORIGIN PIXELS\n    idx2 = K.dot(m,tf.cast(idx,dtype='float32'))\n    idx2 = K.cast(idx2,dtype='int32')\n    idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)\n    # FIND ORIGIN PIXEL VALUES           \n    idx3 = tf.stack( [DIM//2-idx2[0,], DIM//2-1+idx2[1,]] )\n    d = tf.gather_nd(image,tf.transpose(idx3))\n        \n    return tf.reshape(d,[DIM,DIM,3]),label\n\ndef resize_and_crop_image(image, label):\n    w = tf.shape(image)[0]\n    h = tf.shape(image)[1]\n    tw = IMAGE_TARGET[1]\n    th = IMAGE_TARGET[0]\n    resize_crit = (w * th) / (h * tw)\n    image = tf.cond(resize_crit < 1,\n                    lambda: tf.image.resize(image, [w*tw/w, h*tw/w]), # if true\n                    lambda: tf.image.resize(image, [w*th/h, h*th/h])  # if false\n                   )\n    nw = tf.shape(image)[0]\n    nh = tf.shape(image)[1]\n    image = tf.image.crop_to_bounding_box(image, (nw - tw) // 2, (nh - th) // 2, tw, th)\n    return image, label\n\ndef normalize(image, label):\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    image = tf.cast(image, tf.float32)/255.0 \n    # image = tf.image.per_image_standardization(image)\n    return image, label\n\n\ndef augmentation(image, label):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    image = tf.image.rot90(image, k=random.randrange(4))\n    image = tf.image.random_saturation(image, 0, 2)\n    image = tf.image.random_hue(image, 0.08)\n    image = tf.image.random_contrast(image, 0.7, 1.3)\n    image = tf.image.random_brightness(image, 0.15)\n    return image, label\n\n\ndef force_image_sizes(dataset):\n    reshape_images = lambda image, label: (tf.reshape(image, SHAPE), label)   \n    dataset = dataset.map(reshape_images, num_parallel_calls=AUTO)\n    return dataset\n\ndef load_dataset(filenames):\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) \n    dataset = dataset.with_options(ignore_order)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTO)\n    dataset = dataset.map(resize_and_crop_image, num_parallel_calls=AUTO)\n    return dataset\n\n\ndef get_training_dataset():\n    dataset = load_dataset(TRAINING_FILENAMES)\n    dataset = dataset.map(transform, num_parallel_calls=AUTO)\n    dataset = dataset.map(normalize, num_parallel_calls=AUTO)\n#     dataset = force_image_sizes(dataset)\n    dataset = dataset.repeat()\n    dataset = dataset.shuffle(30523)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\ndef get_validation_dataset():\n    dataset = load_dataset(VALIDATION_FILENAMES)\n    dataset = dataset.map(normalize, num_parallel_calls=AUTO)\n#     dataset = force_image_sizes(dataset)\n    dataset = dataset.batch(VALIDATION_BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) \n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.ioff()\nplt.rc('image', cmap='gray_r')\nplt.rc('grid', linewidth=1)\nplt.rc('xtick', top=False, bottom=False, labelsize='large')\nplt.rc('ytick', left=False, right=False, labelsize='large')\nplt.rc('axes', facecolor='F8F8F8', titlesize=\"large\", edgecolor='white')\nplt.rc('text', color='a8151a')\nplt.rc('figure', facecolor='F0F0F0', figsize=(16,9))\n# Matplotlib fonts\nMATPLOTLIB_FONT_DIR = os.path.join(os.path.dirname(plt.__file__), \"mpl-data/fonts/ttf\")\n\ndef plot_learning_rate(lr_func, epochs):\n    xx = np.arange(epochs+1, dtype=np.float)\n    y = [lr_decay(x) for x in xx]\n    fig, ax = plt.subplots(figsize=(9, 6))\n    ax.set_xlabel('epochs')\n    ax.set_title('Learning rate\\ndecays from {:0.3g} to {:0.3g}'.format(y[0], y[-2]))\n    ax.minorticks_on()\n    ax.grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n    ax.grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n    ax.step(xx,y, linewidth=3, where='post')\n    display.display(fig)\n\nclass PlotTraining(tf.keras.callbacks.Callback):\n  def __init__(self, sample_rate=1, zoom=1):\n    self.sample_rate = sample_rate\n    self.step = 0\n    self.zoom = zoom\n    self.steps_per_epoch = STEP_PER_EPOCH\n\n  def on_train_begin(self, logs={}):\n    self.batch_history = {}\n    self.batch_step = []\n    self.epoch_history = {}\n    self.epoch_step = []\n    self.fig, self.axes = plt.subplots(1, 2, figsize=(16, 7))\n    plt.ioff()\n\n  def on_batch_end(self, batch, logs={}):\n    if (batch % self.sample_rate) == 0:\n      self.batch_step.append(self.step)\n      for k,v in logs.items():\n        # do not log \"batch\" and \"size\" metrics that do not change\n        # do not log training accuracy \"acc\"\n        if k=='batch' or k=='size':# or k=='acc':\n          continue\n        self.batch_history.setdefault(k, []).append(v)\n    self.step += 1\n\n  def on_epoch_end(self, epoch, logs={}):\n    plt.close(self.fig)\n    self.axes[0].cla()\n    self.axes[1].cla()\n      \n    self.axes[0].set_ylim(0, 1.2/self.zoom)\n    self.axes[1].set_ylim(1-1/self.zoom/2, 1+0.1/self.zoom/2)\n    \n    self.epoch_step.append(self.step)\n    for k,v in logs.items():\n      # only log validation metrics\n      if not k.startswith('val_'):\n        continue\n      self.epoch_history.setdefault(k, []).append(v)\n\n    display.clear_output(wait=True)\n    \n    for k,v in self.batch_history.items():\n      self.axes[0 if k.endswith('loss') else 1].plot(np.array(self.batch_step) / self.steps_per_epoch, v, label=k)\n      \n    for k,v in self.epoch_history.items():\n      self.axes[0 if k.endswith('loss') else 1].plot(np.array(self.epoch_step) / self.steps_per_epoch, v, label=k, linewidth=3)\n      \n    self.axes[0].legend()\n    self.axes[1].legend()\n    self.axes[0].set_xlabel('epochs')\n    self.axes[1].set_xlabel('epochs')\n    self.axes[0].minorticks_on()\n    self.axes[0].grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n    self.axes[0].grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n    self.axes[1].minorticks_on()\n    self.axes[1].grid(True, which='major', axis='both', linestyle='-', linewidth=1)\n    self.axes[1].grid(True, which='minor', axis='both', linestyle=':', linewidth=0.5)\n    display.display(self.fig)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with strategy.scope():\n    bnmomemtum=0.88\n    def fire(x, filters, kernel_size):\n        if not isinstance(filters, list): \n            filters = [filters, filters]  \n        x = SeparableConv2D(filters[0], kernel_size, padding='same', use_bias=False)(x)\n        x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n        x = Activation('relu')(x)\n        x = SeparableConv2D(filters[1], kernel_size, padding='same', use_bias=False)(x)\n        return BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n    \n    def fire_module_separable_conv(filters, kernel_size=(3, 3)):\n        return lambda x: fire(x, filters, kernel_size)\n    \n    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1\n    img_input = Input(shape=SHAPE)\n\n    x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False)(img_input)\n    x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n    x = Activation('relu')(x)\n\n    x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x)\n    x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n    x = Activation('relu')(x)\n\n    residual = Conv2D(128, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(residual)\n\n    x = fire_module_separable_conv(128)(x)\n\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = add_concat([x, residual])\n\n    residual = Conv2D(256, (1, 1), strides=(2, 2), padding='same', use_bias=False)(x)\n    residual = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(residual)\n\n    x = Activation('relu')(x)\n    x = fire_module_separable_conv(256)(x)\n\n    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)\n    x = add_concat([x, residual])\n\n    for i in range(4):\n        residual = x\n\n        x = Activation('relu')(x)\n        x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False)(x)\n        x = BatchNormalization(axis=channel_axis, center=True, scale=False, momentum=bnmomemtum)(x)\n        x = Activation('relu')(x)\n        x = fire_module_separable_conv(256)(x)\n        \n        x = add_concat([x, residual])\n\n\n    x = fire_module_separable_conv([728, 1024])(x)\n    x = Activation('relu')(x)\n    x = GlobalAveragePooling2D()(x)\n\n    x = Dense(572, activation='softmax')(x)\n    model = Model(inputs=img_input, outputs=[x]) \n    \nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if strategy.num_replicas_in_sync == 1: # single GPU\n    start_lr =  0.0001\n    min_lr = 0.000158\n    max_lr = 0.01 * strategy.num_replicas_in_sync\n    rampup_epochs = 14\n    sustain_epochs = 0\n    exp_decay = .7\nelse: # TPU pod\n    start_lr =  0.0001\n    min_lr = 0.000158\n    max_lr = 0.01 * strategy.num_replicas_in_sync\n    rampup_epochs = 14\n    sustain_epochs = 0\n    exp_decay = .7\n\nEPOCHS=25\n\ndef lr_decay(epoch):\n    def lr(epoch, start_lr, min_lr, max_lr, rampup_epochs, sustain_epochs, exp_decay):\n        if epoch < rampup_epochs:\n            lr = (max_lr - start_lr)/rampup_epochs * epoch + start_lr\n        elif epoch < rampup_epochs + sustain_epochs:\n            lr = max_lr\n        else:\n            lr = (max_lr - min_lr) * exp_decay**(epoch-rampup_epochs-sustain_epochs) + min_lr\n        return lr\n    return lr(epoch, start_lr, min_lr, max_lr, rampup_epochs, sustain_epochs, exp_decay)\n    \nlr_decay_callback = tf.keras.callbacks.LearningRateScheduler(lambda epoch: lr_decay(epoch), verbose=True)\n\nrng = [i for i in range(EPOCHS)]\ny = [lr_decay(x) for x in rng]\nplt.plot(rng, [lr_decay(x) for x in rng])\nprint(y[0], y[-1])\nplot_learning_rate(lr_decay_callback, EPOCHS)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weight_path=\"{}_weights.squeeze.hdf5\".format('model')\n\ncheckpoint = ModelCheckpoint(weight_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min', save_weights_only=True) \n\nplot_training = PlotTraining(sample_rate=10, zoom=1)\ncallbacks_list = [checkpoint, plot_training, lr_decay_callback] #, ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"training_dataset = get_training_dataset()\nvalidation_dataset = get_validation_dataset()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(\n    training_dataset, \n    steps_per_epoch=STEP_PER_EPOCH, \n    epochs=EPOCHS,\n    validation_data=validation_dataset,\n    validation_steps=VALIDATION_STEP_PER_EPOCH,\n    callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights(weight_path)\nmodel.save('model_tpu_Squeeze.h5')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}