{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":18278,"databundleVersionId":968043,"sourceType":"competition"},{"sourceId":2580782,"sourceType":"datasetVersion","datasetId":1404099},{"sourceId":5825477,"sourceType":"datasetVersion","datasetId":3116703},{"sourceId":6531447,"sourceType":"datasetVersion","datasetId":2937277},{"sourceId":7764577,"sourceType":"datasetVersion","datasetId":4541447},{"sourceId":7796949,"sourceType":"datasetVersion","datasetId":4564769},{"sourceId":7858633,"sourceType":"datasetVersion","datasetId":4609649},{"sourceId":7871487,"sourceType":"datasetVersion","datasetId":4618799},{"sourceId":7872370,"sourceType":"datasetVersion","datasetId":4619373},{"sourceId":7880344,"sourceType":"datasetVersion","datasetId":4625079},{"sourceId":7882269,"sourceType":"datasetVersion","datasetId":4626432},{"sourceId":7933622,"sourceType":"datasetVersion","datasetId":4663524},{"sourceId":7987204,"sourceType":"datasetVersion","datasetId":4701680},{"sourceId":7996671,"sourceType":"datasetVersion","datasetId":4708379},{"sourceId":8004313,"sourceType":"datasetVersion","datasetId":4713937},{"sourceId":8069602,"sourceType":"datasetVersion","datasetId":4761260}],"dockerImageVersionId":30628,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import math, re, os\nimport tensorflow as tf\nimport numpy as np\nimport jax\nfrom matplotlib import pyplot as plt\nfrom kaggle_datasets import KaggleDatasets\nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\n#from tf.keras.utils import multi_gpu_model\nprint(\"Tensorflow version \" + tf.__version__)\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:54:13.469938Z","iopub.execute_input":"2024-03-09T05:54:13.470307Z","iopub.status.idle":"2024-03-09T05:54:13.476571Z","shell.execute_reply.started":"2024-03-09T05:54:13.470274Z","shell.execute_reply":"2024-03-09T05:54:13.47544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/swintransformertf')\nfrom swintransformer import SwinTransformer","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:54:16.910823Z","iopub.execute_input":"2024-03-09T05:54:16.911183Z","iopub.status.idle":"2024-03-09T05:54:17.049023Z","shell.execute_reply.started":"2024-03-09T05:54:16.911154Z","shell.execute_reply":"2024-03-09T05:54:17.048293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TPU or GPU detection","metadata":{}},{"cell_type":"code","source":"# NEW on TPU in TensorFlow 24: shorter cross-compatible TPU/GPU/multi-GPU/cluster-GPU detection code\n\ntry: # detect TPUs\n    resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='local')\n    tf.tpu.experimental.initialize_tpu_system(resolver)\n    strategy = tf.distribute.TPUStrategy(resolver)\nexcept ValueError: # detect GPUs\n    strategy = tf.distribute.MirroredStrategy() # for GPU or multi-GPU machines\n    #strategy = tf.distribute.get_strategy() # default strategy that works on CPU and single GPU\n    #strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy() # for clusters of multi-GPU machines\n\nprint(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:54:20.82422Z","iopub.execute_input":"2024-03-09T05:54:20.825104Z","iopub.status.idle":"2024-03-09T05:54:21.983624Z","shell.execute_reply.started":"2024-03-09T05:54:20.82507Z","shell.execute_reply":"2024-03-09T05:54:21.982323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strategy = tf.distribute.MirroredStrategy() # for GPU or multi-GPU machines","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:54:55.188753Z","iopub.execute_input":"2024-03-09T05:54:55.189133Z","iopub.status.idle":"2024-03-09T05:54:55.76932Z","shell.execute_reply.started":"2024-03-09T05:54:55.189102Z","shell.execute_reply":"2024-03-09T05:54:55.768416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:05.923819Z","iopub.execute_input":"2024-03-09T05:55:05.924142Z","iopub.status.idle":"2024-03-09T05:55:05.929043Z","shell.execute_reply.started":"2024-03-09T05:55:05.924117Z","shell.execute_reply":"2024-03-09T05:55:05.928108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Competition data access\nTPUs read data directly from Google Cloud Storage (GCS). This Kaggle utility will copy the dataset to a GCS bucket co-located with the TPU. If you have multiple datasets attached to the notebook, you can pass the name of a specific dataset to the get_gcs_path function. The name of the dataset is the name of the directory it is mounted in. Use `!ls /kaggle/input/` to list attached datasets.","metadata":{}},{"cell_type":"code","source":"GCS_DS_PATH = KaggleDatasets().get_gcs_path(\"flower-classification\") # you can list the bucket with \"!gsutil ls $GCS_DS_PATH\"","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:10.945529Z","iopub.execute_input":"2024-03-09T05:55:10.946427Z","iopub.status.idle":"2024-03-09T05:55:11.411661Z","shell.execute_reply.started":"2024-03-09T05:55:10.946391Z","shell.execute_reply":"2024-03-09T05:55:11.410664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Configuration","metadata":{}},{"cell_type":"code","source":"IMAGE_SIZE = [224, 224] # At this size, a GPU will run out of memory. Use the TPU.\n                        # For GPU training, please select 224 x 224 px image size.\nEPOCHS = 15\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync\n\nGCS_PATH_SELECT = { # available image sizes\n    192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',\n    224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',\n    331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',\n    512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'\n}\nGCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]\n\nTRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')\nVALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')\nTEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec') # predictions on this dataset should be submitted for the competition\n\nCLASSES = ['pink primrose',    'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea',     'wild geranium',     'tiger lily',           'moon orchid',              'bird of paradise', 'monkshood',        'globe thistle',         # 00 - 09\n           'snapdragon',       \"colt's foot\",               'king protea',      'spear thistle', 'yellow iris',       'globe-flower',         'purple coneflower',        'peruvian lily',    'balloon flower',   'giant white arum lily', # 10 - 19\n           'fire lily',        'pincushion flower',         'fritillary',       'red ginger',    'grape hyacinth',    'corn poppy',           'prince of wales feathers', 'stemless gentian', 'artichoke',        'sweet william',         # 20 - 29\n           'carnation',        'garden phlox',              'love in the mist', 'cosmos',        'alpine sea holly',  'ruby-lipped cattleya', 'cape flower',              'great masterwort', 'siam tulip',       'lenten rose',           # 30 - 39\n           'barberton daisy',  'daffodil',                  'sword lily',       'poinsettia',    'bolero deep blue',  'wallflower',           'marigold',                 'buttercup',        'daisy',            'common dandelion',      # 40 - 49\n           'petunia',          'wild pansy',                'primula',          'sunflower',     'lilac hibiscus',    'bishop of llandaff',   'gaura',                    'geranium',         'orange dahlia',    'pink-yellow dahlia',    # 50 - 59\n           'cautleya spicata', 'japanese anemone',          'black-eyed susan', 'silverbush',    'californian poppy', 'osteospermum',         'spring crocus',            'iris',             'windflower',       'tree poppy',            # 60 - 69\n           'gazania',          'azalea',                    'water lily',       'rose',          'thorn apple',       'morning glory',        'passion flower',           'lotus',            'toad lily',        'anthurium',             # 70 - 79\n           'frangipani',       'clematis',                  'hibiscus',         'columbine',     'desert-rose',       'tree mallow',          'magnolia',                 'cyclamen ',        'watercress',       'canna lily',            # 80 - 89\n           'hippeastrum ',     'bee balm',                  'pink quill',       'foxglove',      'bougainvillea',     'camellia',             'mallow',                   'mexican petunia',  'bromelia',         'blanket flower',        # 90 - 99\n           'trumpet creeper',  'blackberry lily',           'common tulip',     'wild rose']                                                                                                                                               # 100 - 102","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:14.117669Z","iopub.execute_input":"2024-03-09T05:55:14.118328Z","iopub.status.idle":"2024-03-09T05:55:14.586807Z","shell.execute_reply.started":"2024-03-09T05:55:14.118295Z","shell.execute_reply":"2024-03-09T05:55:14.586058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization utilities\ndata -> pixels, nothing of much interest for the machine learning practitioner in this section.","metadata":{}},{"cell_type":"code","source":"# numpy and matplotlib defaults\nnp.set_printoptions(threshold=15, linewidth=80)\n\ndef batch_to_numpy_images_and_labels(data):\n    images, labels = data\n    numpy_images = images.numpy()\n    numpy_labels = labels.numpy()\n    if numpy_labels.dtype == object: # binary string in this case, these are image ID strings\n        numpy_labels = [None for _ in enumerate(numpy_images)]\n    # If no labels, only image IDs, return None for labels (this is the case for test data)\n    return numpy_images, numpy_labels\n\ndef title_from_label_and_target(label, correct_label):\n    if correct_label is None:\n        return CLASSES[label], True\n    correct = (label == correct_label)\n    return \"{} [{}{}{}]\".format(CLASSES[label], 'OK' if correct else 'NO', u\"\\u2192\" if not correct else '',\n                                CLASSES[correct_label] if not correct else ''), correct\n\ndef display_one_flower(image, title, subplot, red=False, titlesize=16):\n    plt.subplot(*subplot)\n    plt.axis('off')\n    plt.imshow(image)\n    if len(title) > 0:\n        plt.title(title, fontsize=int(titlesize) if not red else int(titlesize/1.2), color='red' if red else 'black', fontdict={'verticalalignment':'center'}, pad=int(titlesize/1.5))\n    return (subplot[0], subplot[1], subplot[2]+1)\n    \ndef display_batch_of_images(databatch, predictions=None):\n    \"\"\"This will work with:\n    display_batch_of_images(images)\n    display_batch_of_images(images, predictions)\n    display_batch_of_images((images, labels))\n    display_batch_of_images((images, labels), predictions)\n    \"\"\"\n    # data\n    images, labels = batch_to_numpy_images_and_labels(databatch)\n    if labels is None:\n        labels = [None for _ in enumerate(images)]\n        \n    # auto-squaring: this will drop data that does not fit into square or square-ish rectangle\n    rows = int(math.sqrt(len(images)))\n    cols = len(images)//rows\n        \n    # size and spacing\n    FIGSIZE = 13.0\n    SPACING = 0.1\n    subplot=(rows,cols,1)\n    if rows < cols:\n        plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))\n    else:\n        plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))\n    \n    # display\n    for i, (image, label) in enumerate(zip(images[:rows*cols], labels[:rows*cols])):\n        title = '' if label is None else CLASSES[label]\n        correct = True\n        if predictions is not None:\n            title, correct = title_from_label_and_target(predictions[i], label)\n        dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3 # magic formula tested to work from 1x1 to 10x10 images\n        subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)\n    \n    #layout\n    plt.tight_layout()\n    if label is None and predictions is None:\n        plt.subplots_adjust(wspace=0, hspace=0)\n    else:\n        plt.subplots_adjust(wspace=SPACING, hspace=SPACING)\n    plt.show()\n\ndef display_confusion_matrix(cmat, score, precision, recall):\n    plt.figure(figsize=(15,15))\n    ax = plt.gca()\n    ax.matshow(cmat, cmap='Reds')\n    ax.set_xticks(range(len(CLASSES)))\n    ax.set_xticklabels(CLASSES, fontdict={'fontsize': 7})\n    plt.setp(ax.get_xticklabels(), rotation=45, ha=\"left\", rotation_mode=\"anchor\")\n    ax.set_yticks(range(len(CLASSES)))\n    ax.set_yticklabels(CLASSES, fontdict={'fontsize': 7})\n    plt.setp(ax.get_yticklabels(), rotation=45, ha=\"right\", rotation_mode=\"anchor\")\n    titlestring = \"\"\n    if score is not None:\n        titlestring += 'f1 = {:.3f} '.format(score)\n    if precision is not None:\n        titlestring += '\\nprecision = {:.3f} '.format(precision)\n    if recall is not None:\n        titlestring += '\\nrecall = {:.3f} '.format(recall)\n    if len(titlestring) > 0:\n        ax.text(101, 1, titlestring, fontdict={'fontsize': 18, 'horizontalalignment':'right', 'verticalalignment':'top', 'color':'#804040'})\n    plt.show()\n    \ndef display_training_curves(training, validation, title, subplot):\n    if subplot%10==1: # set up the subplots on the first call\n        plt.subplots(figsize=(10,10), facecolor='#F0F0F0')\n        plt.tight_layout()\n    ax = plt.subplot(subplot)\n    ax.set_facecolor('#F8F8F8')\n    ax.plot(training)\n    ax.plot(validation)\n    ax.set_title('model '+ title)\n    ax.set_ylabel(title)\n    #ax.set_ylim(0.28,1.05)\n    ax.set_xlabel('epoch')\n    ax.legend(['train', 'valid.'])","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:18.750705Z","iopub.execute_input":"2024-03-09T05:55:18.751876Z","iopub.status.idle":"2024-03-09T05:55:18.77895Z","shell.execute_reply.started":"2024-03-09T05:55:18.751838Z","shell.execute_reply":"2024-03-09T05:55:18.777875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Datasets","metadata":{}},{"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)  # image format uint8 [0,255]\n    image = tf.reshape(image, [*IMAGE_SIZE, 3]) # explicit size needed for TPU\n    return image\n\ndef read_labeled_tfrecord(example):\n    LABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"class\": tf.io.FixedLenFeature([], tf.int64),  # shape [] means single element\n    }\n    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    label = tf.cast(example['class'], tf.int32)\n    return image, label # returns a dataset of (image, label) pairs\n\ndef read_unlabeled_tfrecord(example):\n    UNLABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"id\": tf.io.FixedLenFeature([], tf.string),  # shape [] means single element\n        # class is missing, this competitions's challenge is to predict flower classes for the test dataset\n    }\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    idnum = example['id']\n    return image, idnum # returns a dataset of image(s)\n\ndef load_dataset(filenames, labeled=True, ordered=False):\n    # Read from TFRecords. For optimal performance, reading from multiple files at once and\n    # disregarding data order. Order does not matter since we will be shuffling the data anyway.\n\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # automatically interleaves reads from multiple files\n    dataset = dataset.with_options(ignore_order) # uses data as soon as it streams in, rather than in its original order\n    dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)\n    # returns a dataset of (image, label) pairs if labeled=True or (image, id) pairs if labeled=False\n    return dataset\n\ndef data_augment(image, label):\n    # data augmentation. Thanks to the dataset.prefetch(AUTO) statement in the next function (below),\n    # this happens essentially for free on TPU. Data pipeline code is executed on the \"CPU\" part\n    # of the TPU while the TPU itself is computing gradients.\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_saturation(image, 0, 2)\n    tf.image.random_jpeg_quality(image,50,70)\n    return image, label   \n\ndef get_training_dataset():\n    dataset = load_dataset(TRAINING_FILENAMES, labeled=True)\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(2048)\n    dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\ndef get_validation_dataset(ordered=False):\n    dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)\n    dataset = dataset.cache()\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\ndef get_test_dataset(ordered=False):\n    dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\ndef count_data_items(filenames):\n    # the number of data items is written in the name of the .tfrec files, i.e. flowers00-230.tfrec = 230 data items\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)\n\nNUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)\nNUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)\nNUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)\nSTEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE\nVALIDATION_STEPS = -(-NUM_VALIDATION_IMAGES // BATCH_SIZE) # The \"-(-//)\" trick rounds up instead of down :-)\nTEST_STEPS = -(-NUM_TEST_IMAGES // BATCH_SIZE)             # The \"-(-//)\" trick rounds up instead of down :-)\nprint('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-03-14T02:16:42.415365Z","iopub.execute_input":"2024-03-14T02:16:42.415687Z","iopub.status.idle":"2024-03-14T02:16:42.468785Z","shell.execute_reply.started":"2024-03-14T02:16:42.41566Z","shell.execute_reply":"2024-03-14T02:16:42.467499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset visualizations","metadata":{}},{"cell_type":"code","source":"# data dump\nprint(\"Training data shapes:\")\nfor image, label in get_training_dataset().take(3):\n    print(image.numpy().shape, label.numpy().shape)\nprint(\"Training data label examples:\", label.numpy())\nprint(\"Validation data shapes:\")\nfor image, label in get_validation_dataset().take(3):\n    print(image.numpy().shape, label.numpy().shape)\nprint(\"Validation data label examples:\", label.numpy())\nprint(\"Test data shapes:\")\nfor image, idnum in get_test_dataset().take(3):\n    print(image.numpy().shape, idnum.numpy().shape)\nprint(\"Test data IDs:\", idnum.numpy().astype('U')) # U=unicode string","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:30.538812Z","iopub.execute_input":"2024-03-09T05:55:30.539762Z","iopub.status.idle":"2024-03-09T05:55:36.205232Z","shell.execute_reply.started":"2024-03-09T05:55:30.539726Z","shell.execute_reply":"2024-03-09T05:55:36.204127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Peek at training data\ntraining_dataset = get_training_dataset()\ntraining_dataset = training_dataset.unbatch().batch(20)\ntrain_batch = iter(training_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:42.668758Z","iopub.execute_input":"2024-03-09T05:55:42.669654Z","iopub.status.idle":"2024-03-09T05:55:42.758238Z","shell.execute_reply.started":"2024-03-09T05:55:42.669615Z","shell.execute_reply":"2024-03-09T05:55:42.757463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run this cell again for next set of images\ndisplay_batch_of_images(next(train_batch))","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:46.529382Z","iopub.execute_input":"2024-03-09T05:55:46.529731Z","iopub.status.idle":"2024-03-09T05:55:51.265931Z","shell.execute_reply.started":"2024-03-09T05:55:46.529705Z","shell.execute_reply":"2024-03-09T05:55:51.264566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# peer at test data\ntest_dataset = get_test_dataset()\ntest_dataset = test_dataset.unbatch().batch(20)\ntest_batch = iter(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:55:59.345491Z","iopub.execute_input":"2024-03-09T05:55:59.346286Z","iopub.status.idle":"2024-03-09T05:55:59.403199Z","shell.execute_reply.started":"2024-03-09T05:55:59.346241Z","shell.execute_reply":"2024-03-09T05:55:59.402464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run this cell again for next set of images\ndisplay_batch_of_images(next(test_batch))","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:56:04.979114Z","iopub.execute_input":"2024-03-09T05:56:04.979988Z","iopub.status.idle":"2024-03-09T05:56:07.433128Z","shell.execute_reply.started":"2024-03-09T05:56:04.979957Z","shell.execute_reply":"2024-03-09T05:56:07.431693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model\nYou can select these models:  \n`swin_tiny_224`    \n`swin_small_224`  \n`swin_base_224`  \n`swin_base_384`  \n`swin_large_224`  \n`swin_large_384`  ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    img_adjust_layer = tf.keras.layers.Lambda(lambda data: tf.keras.applications.imagenet_utils.preprocess_input(tf.cast(data, tf.float32), mode=\"torch\"), input_shape=[*IMAGE_SIZE, 3])\n    pretrained_model = SwinTransformer('swin_large_224', num_classes=len(CLASSES), include_top=False, pretrained=True, use_tpu=False)\n    \n    modelswin = tf.keras.Sequential([\n        img_adjust_layer,\n        pretrained_model,\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])\n    # topk = tf.keras.metrics.TopKCategoricalAccuracy(3, name=\"top-3-accuracy\")\n        \nmodelswin.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5, epsilon=1e-8),\n    loss = 'sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\nmodelswin.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:56:17.089402Z","iopub.execute_input":"2024-03-09T05:56:17.089796Z","iopub.status.idle":"2024-03-09T05:56:54.288314Z","shell.execute_reply.started":"2024-03-09T05:56:17.089765Z","shell.execute_reply":"2024-03-09T05:56:54.287299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# instantiating the model in the strategy scope creates the model on the TPU\nwith strategy.scope():\n  #pretrained_model = tf.keras.applications.DenseNet201(input_shape=[512, 512, 3], include_top=False)\n  #pretrained_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False ,input_shape=[*IMAGE_SIZE, 3])\n  pretrained_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3])\n  #pretrained_model = tf.keras.applications.MobileNet(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3])\n  pretrained_model.trainable = True\n\n  model = tf.keras.Sequential([\n      pretrained_model,\n        \n      # tf.keras.layers.Conv2D(kernel_size=3, filters=16, padding='same', activation='relu', input_shape=[TARGET_SIZE,TARGET_SIZE, 3]),\n      # tf.keras.layers.Conv2D(kernel_size=3, filters=32, padding='same', activation='relu'),\n      # tf.keras.layers.MaxPooling2D(pool_size=2),\n      \n      # tf.keras.layers.Conv2D(kernel_size=3, filters=64, padding='same', activation='relu'),\n      # tf.keras.layers.MaxPooling2D(pool_size=2),\n      \n      # tf.keras.layers.Conv2D(kernel_size=3, filters=128, padding='same', activation='relu'),\n      # tf.keras.layers.MaxPooling2D(pool_size=2),\n      \n      # tf.keras.layers.Conv2D(kernel_size=3, filters=256, padding='same', activation='relu'),\n      \n      tf.keras.layers.GlobalAveragePooling2D(),\n#       tf.keras.layers.Dense(128, 'relu'),\n#       tf.keras.layers.Dense(256, 'relu'),\n#       tf.keras.layers.Dense(256, 'relu'),\n      tf.keras.layers.Dense(104,'softmax')])\n  #EfficientNet_flower_clf.weights.h5\n  checkpoint1 = tf.keras.callbacks.ModelCheckpoint('ResNet50.weights.h5', verbose=1, monitor='val_loss', mode='min', \n                                        save_best_only=True, save_weights_only=True)\n\n  checkpoint2 = tf.keras.callbacks.ModelCheckpoint('DenseNet_flower_clf.weights.h5', verbose=1, monitor='val_loss', mode='min', save_best_only=True, save_weights_only=True)                                      \n  model.compile(optimizer='adagrad',\n              loss='sparse_categorical_crossentropy',\n              metrics=['sparse_categorical_accuracy'])\n  # model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.1), \n  #               loss='sparse_categorical_crossentropy',\n  #               metrics=['sparse_categorical_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:56:54.290408Z","iopub.execute_input":"2024-03-09T05:56:54.290778Z","iopub.status.idle":"2024-03-09T05:56:59.937712Z","shell.execute_reply.started":"2024-03-09T05:56:54.290742Z","shell.execute_reply":"2024-03-09T05:56:59.936946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('/kaggle/working/tpu_flowerResNet50.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:59:27.407161Z","iopub.execute_input":"2024-03-09T05:59:27.407895Z","iopub.status.idle":"2024-03-09T05:59:27.932699Z","shell.execute_reply.started":"2024-03-09T05:59:27.407864Z","shell.execute_reply":"2024-03-09T05:59:27.931741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelswin.save_weights('/kaggle/working/tpu_flowerswin.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-07T04:24:00.299383Z","iopub.execute_input":"2024-03-07T04:24:00.299795Z","iopub.status.idle":"2024-03-07T04:24:03.015906Z","shell.execute_reply.started":"2024-03-07T04:24:00.299759Z","shell.execute_reply":"2024-03-07T04:24:03.014814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/tpu_flowerResNet50Full.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-07T04:24:36.892181Z","iopub.execute_input":"2024-03-07T04:24:36.892523Z","iopub.status.idle":"2024-03-07T04:24:38.488229Z","shell.execute_reply.started":"2024-03-07T04:24:36.892493Z","shell.execute_reply":"2024-03-07T04:24:38.48707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelswin.save('/kaggle/working/tpu_flowerswinFull.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-07T04:24:56.048554Z","iopub.execute_input":"2024-03-07T04:24:56.049356Z","iopub.status.idle":"2024-03-07T04:25:01.327667Z","shell.execute_reply.started":"2024-03-07T04:24:56.049322Z","shell.execute_reply":"2024-03-07T04:25:01.326633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"#multi card run\nEPOCHS =30\nhistory = model.fit(get_training_dataset(), steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS,\n                    validation_data=get_validation_dataset(), validation_steps=VALIDATION_STEPS)","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:56:59.939183Z","iopub.execute_input":"2024-03-09T05:56:59.939493Z","iopub.status.idle":"2024-03-09T05:58:15.09624Z","shell.execute_reply.started":"2024-03-09T05:56:59.939469Z","shell.execute_reply":"2024-03-09T05:58:15.09475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = modelswin.fit(get_training_dataset(), steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS,\n                    validation_data=get_validation_dataset(), validation_steps=VALIDATION_STEPS)","metadata":{"execution":{"iopub.status.busy":"2024-03-07T04:01:55.661207Z","iopub.execute_input":"2024-03-07T04:01:55.661903Z","iopub.status.idle":"2024-03-07T04:16:50.542354Z","shell.execute_reply.started":"2024-03-07T04:01:55.661867Z","shell.execute_reply":"2024-03-07T04:16:50.54092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = modelswin.fit(get_training_dataset(), steps_per_epoch=10, epochs=EPOCHS,\n                    validation_data=get_validation_dataset(), validation_steps=VALIDATION_STEPS)","metadata":{"execution":{"iopub.status.busy":"2024-03-07T04:21:20.135848Z","iopub.execute_input":"2024-03-07T04:21:20.136259Z","iopub.status.idle":"2024-03-07T04:23:49.919518Z","shell.execute_reply.started":"2024-03-07T04:21:20.136226Z","shell.execute_reply":"2024-03-07T04:23:49.917873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_training_curves(history.history['loss'], history.history['val_loss'], 'loss', 211)\ndisplay_training_curves(history.history['accuracy'], history.history['val_accuracy'], 'accuracy', 212)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T00:20:51.097196Z","iopub.execute_input":"2023-12-17T00:20:51.097526Z","iopub.status.idle":"2023-12-17T00:20:51.630998Z","shell.execute_reply.started":"2023-12-17T00:20:51.097493Z","shell.execute_reply":"2023-12-17T00:20:51.62992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion matrix","metadata":{}},{"cell_type":"code","source":"cmdataset = get_validation_dataset(ordered=True) # since we are splitting the dataset and iterating separately on images and labels, order matters.\nimages_ds = cmdataset.map(lambda image, label: image)\nlabels_ds = cmdataset.map(lambda image, label: label).unbatch()\ncm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES))).numpy() # get everything as one batch\ncm_probabilities = model.predict(images_ds, steps=VALIDATION_STEPS)\ncm_predictions = np.argmax(cm_probabilities, axis=-1)\nprint(\"Correct   labels: \", cm_correct_labels.shape, cm_correct_labels)\nprint(\"Predicted labels: \", cm_predictions.shape, cm_predictions)","metadata":{"execution":{"iopub.status.busy":"2023-12-17T00:20:51.632271Z","iopub.execute_input":"2023-12-17T00:20:51.632569Z","iopub.status.idle":"2023-12-17T00:21:16.870208Z","shell.execute_reply.started":"2023-12-17T00:20:51.632526Z","shell.execute_reply":"2023-12-17T00:21:16.868958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))\nscore = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\nprecision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\nrecall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)), average='macro')\ncmat = (cmat.T / cmat.sum(axis=1)).T # normalized\ndisplay_confusion_matrix(cmat, score, precision, recall)\nprint('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))","metadata":{"execution":{"iopub.status.busy":"2023-12-17T00:21:16.871466Z","iopub.execute_input":"2023-12-17T00:21:16.871928Z","iopub.status.idle":"2023-12-17T00:21:18.395577Z","shell.execute_reply.started":"2023-12-17T00:21:16.871892Z","shell.execute_reply":"2023-12-17T00:21:18.394327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Face recognition","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import random\nrandom.seed(5)\nnp.random.seed(5)\ntf.random.set_seed(5)\nfilenames = tf.data.TFRecordDataset.list_files(\"/kaggle/input/faces-ms1m-refine-v2-112x112-tfrecord/faces_ms1m_refine_v2_112x112-*.tfrecord\")\n\ntrain_ds = tf.data.TFRecordDataset(filenames, num_parallel_reads = tf.data.AUTOTUNE)\n\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = {'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_tfrecord_fn(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.image.resize(img, (80, 80))\n    #img = tf.cast(img, dtype=tf.float32)\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)\n    return img, label","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img,label in train_ds.take(3):\n  print(label)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img,label in train_ds.take(3):\n  print(label)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting first 5 examples\nfrom matplotlib import pyplot as plt\nplt.figure(figsize=(6, 6))\nplt.axis('off')\nfor idx, (image, label) in enumerate(train_ds.take(6)):\n\n    ax = plt.subplot(2, 3, idx + 1)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    image = image / 255.0    \n    \n    # Plot the image\n    plt.imshow(image)\n    plt.title(f\"Label: {label}\")\n    plt.axis('off')\nplt.tight_layout()\n# plt.subplots_adjust(wspace=0.1, hspace=0.1)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting first 5 examples\nfrom matplotlib import pyplot as plt\nplt.figure(figsize=(6, 6))\nplt.axis('off')\nfor idx, (image, label) in enumerate(train_ds.take(6)):\n\n    ax = plt.subplot(2, 3, idx + 1)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    imagesave = np.array(tf.cast(image, dtype=tf.uint8))\n    #print(imagesave)\n    # Convert image from [0, 255] range to [0, 1] for plt\n    image = image / 255.0\n    \n    # Plot the image\n    plt.imshow(image)\n    plt.title(f\"Label: {label}\")\n    cv2.imwrite(os.path.join(save_dir, str(int(label))+'.jpg'), imagesave)\n    plt.axis('off')\nplt.tight_layout()\n# plt.subplots_adjust(wspace=0.1, hspace=0.1)\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nameslist = os.listdir(os.path.join('/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/', 'faces-ms1m-refine-v2-80x80'))\nimage = []\nfor name in nameslist[0:6]:\n    print(name)\n    imageread = cv2.imread('/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/faces-ms1m-refine-v2-80x80/'+name)\n    image.append(imageread)\n \n\nplt.figure(figsize=(10,10))\n#image = image / 255.0\nplt.subplot(1,3,1)\nplt.imshow(image[0])\nplt.subplot(1,3,2)\nplt.imshow(image[1])\nplt.subplot(1,3,3)\nplt.imshow(image[2])\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nameslist = os.listdir(os.path.join('/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/', 'faces-ms1m-refine-v2-80x80'))\nimage = []\nfor name in nameslist[0:6]:\n    print(name)\n    imageread = cv2.imread('/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/faces-ms1m-refine-v2-80x80/'+name)\n    image.append(imageread)\n \n\nplt.figure(figsize=(10,10))\n#image = image / 255.0\nplt.subplot(1,3,1)\nplt.imshow(image[0])\nplt.subplot(1,3,2)\nplt.imshow(image[1])\nplt.subplot(1,3,3)\nplt.imshow(image[2])\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nameslist = os.listdir(os.path.join('/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/', 'faces-ms1m-refine-v2-80x80'))\nimage = []\nfor name in nameslist[0:6]:\n    print(name)\n    imageread = cv2.imread('/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/faces-ms1m-refine-v2-80x80/'+name)\n    image.append(imageread)\n \n\nplt.figure(figsize=(10,10))\n#image = image / 255.0\nplt.subplot(1,3,1)\nplt.imshow(image[0])\nplt.subplot(1,3,2)\nplt.imshow(image[1])\nplt.subplot(1,3,3)\nplt.imshow(image[2])\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -cvf /kaggle/working/faces-ms1m-refine-v2-80x80_BGR_2.tar   /kaggle/tmp/faces-ms1m-refine-v2-80x80 | wc -l","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls  -l /kaggle/tmp/faces-ms1m-refine-v2-80x80  | grep \"^-\" | wc -l","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport cv2\nrandom.seed(5)\nnp.random.seed(5)\ntf.random.set_seed(5)\n#os.mkdir('/kaggle/tmp/faces-ms1m-refine-v2-80x80/') \nfilenames = tf.data.TFRecordDataset.list_files(\"/kaggle/input/faces-ms1m-refine-v2-112x112-tfrecord/faces_ms1m_refine_v2_112x112-*.tfrecord\")\n\ntrain_ds = tf.data.TFRecordDataset(filenames, num_parallel_reads = tf.data.AUTOTUNE)\n\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = { 'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}\ndef parse_tfrecord_fn(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.image.resize(img, (80, 80))\n    #img = tf.cast(img, dtype=tf.float32)\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)    \n    return img, label\ntrain_ds = train_ds.map(parse_tfrecord_fn,num_parallel_calls = tf.data.AUTOTUNE)\n\nimagescount ={}\n#os.mkdir('/kaggle/tmp/') \n#os.mkdir('/kaggle/tmp/faces-ms1m-refine-v2-80x80/') \nsave_dir = '/kaggle/tmp/faces-ms1m-refine-v2-80x80/'  \n\nnum_classes = 85742\ntotal_examples = 5822653\n#imagesall  ={}\nnameexec = ''\nfor idx, (image, label) in enumerate(train_ds.take(total_examples)):\n    label = int(label)\n    if label in imagescount.keys():\n        if imagescount[label]<=11:\n            imagescount[label] = imagescount[label]+1           \n            #imagesave = np.array(tf.cast(image, dtype=tf.uint8))\n            #imageshow = cv2.cvtColor(imagesave, cv2.COLOR_RGB2BGR)\n            #cv2.imwrite(os.path.join(save_dir, str(int(label))+'_'+str(imagescount[label])+'.jpg'), imageshow)\n        else:\n            if (imagescount[label]<=21):\n                imagescount[label] = imagescount[label]+1  \n                imagesave = np.array(tf.cast(image, dtype=tf.uint8))\n                imageshow = cv2.cvtColor(imagesave, cv2.COLOR_RGB2BGR)\n                cv2.imwrite(os.path.join(save_dir, str(int(label))+'_'+str(imagescount[label])+'.jpg'), imageshow)\n            #imagesall[nameexec] = image\n    else:        \n        imagescount[label] = 1\n        #imagesave = np.array(tf.cast(image, dtype=tf.uint8))\n        #imageshow = cv2.cvtColor(imagesave, cv2.COLOR_RGB2BGR)\n        #cv2.imwrite(os.path.join(save_dir, str(int(label))+'_'+str(imagescount[label])+'.jpg'), imageshow)\n    if idx%3000==2999:\n        print(idx)\n    ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport cv2\nrandom.seed(5)\nnp.random.seed(5)\ntf.random.set_seed(5)\nos.mkdir('/kaggle/tmp/faces-ms1m-refine-v2-80x80_test/') \nfilenames = tf.data.TFRecordDataset.list_files(\"/kaggle/input/faces-ms1m-refine-v2-112x112-tfrecord/faces_ms1m_refine_v2_112x112-*.tfrecord\")\n\ntrain_ds = tf.data.TFRecordDataset(filenames, num_parallel_reads = tf.data.AUTOTUNE)\n\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = { 'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}\ndef parse_tfrecord_fn(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.image.resize(img, (80, 80))\n    #img = tf.cast(img, dtype=tf.float32)\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)    \n    return img, label\ntrain_ds = train_ds.map(parse_tfrecord_fn,num_parallel_calls = tf.data.AUTOTUNE)\n\nimagescount ={}\n#os.mkdir('/kaggle/tmp/') \n#os.mkdir('/kaggle/tmp/faces-ms1m-refine-v2-80x80/') \nsave_dir = '/kaggle/tmp/faces-ms1m-refine-v2-80x80_test/'  \n\nnum_classes = 85742\ntotal_examples = 5822653\n#imagesall  ={}\nnameexec = ''\nfor idx, (image, label) in enumerate(train_ds.take(total_examples)):\n    label = int(label)\n    if label in imagescount.keys():\n        if imagescount[label]<=8:\n            imagescount[label] = imagescount[label]+1           \n            #imagesave = np.array(tf.cast(image, dtype=tf.uint8))\n            #imageshow = cv2.cvtColor(imagesave, cv2.COLOR_RGB2BGR)\n            #cv2.imwrite(os.path.join(save_dir, str(int(label))+'_'+str(imagescount[label])+'.jpg'), imageshow)\n        else:\n            if (imagescount[label]<=11):\n                imagescount[label] = imagescount[label]+1  \n                imagesave = np.array(tf.cast(image, dtype=tf.uint8))\n                imageshow = cv2.cvtColor(imagesave, cv2.COLOR_RGB2BGR)\n                cv2.imwrite(os.path.join(save_dir, str(int(label))+'_'+str(imagescount[label])+'.jpg'), imageshow)\n            #imagesall[nameexec] = image\n    else:        \n        imagescount[label] = 1\n        #imagesave = np.array(tf.cast(image, dtype=tf.uint8))\n        #imageshow = cv2.cvtColor(imagesave, cv2.COLOR_RGB2BGR)\n        #cv2.imwrite(os.path.join(save_dir, str(int(label))+'_'+str(imagescount[label])+'.jpg'), imageshow)\n    if idx%3000==2999:\n        print(idx)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import backend, layers, metrics\n\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import Xception\nfrom tensorflow.keras.models import Model, Sequential\n\nfrom tensorflow.keras.utils import plot_model\nfrom sklearn.metrics import accuracy_score, confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:43:45.494279Z","iopub.execute_input":"2024-03-09T10:43:45.494989Z","iopub.status.idle":"2024-03-09T10:43:45.590098Z","shell.execute_reply.started":"2024-03-09T10:43:45.494955Z","shell.execute_reply":"2024-03-09T10:43:45.589262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_encoder(input_shape):\n    \"\"\" Returns the image encoding model \"\"\"\n\n    pretrained_model = Xception(\n        input_shape=input_shape,\n        weights='imagenet',\n        include_top=False,\n        pooling='avg',\n    )\n    \n    for i in range(len(pretrained_model.layers)-27):\n        pretrained_model.layers[i].trainable = False\n\n    encode_model = Sequential([\n        pretrained_model,\n        layers.Flatten(),\n        layers.Dense(512, activation='relu'),\n        layers.BatchNormalization(),\n        layers.Dense(256, activation=\"relu\"),\n        layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))\n    ], name=\"Encode_Model\")\n    return encode_model","metadata":{"execution":{"iopub.status.busy":"2024-03-09T10:43:48.271594Z","iopub.execute_input":"2024-03-09T10:43:48.272357Z","iopub.status.idle":"2024-03-09T10:43:48.280023Z","shell.execute_reply.started":"2024-03-09T10:43:48.272325Z","shell.execute_reply":"2024-03-09T10:43:48.278968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainceleb = False\nif trainceleb:\n    size = 160\nelse:\n    size = 80\nclass DistanceLayer(layers.Layer):\n    # A layer to compute ‖f(A) - f(P)‖² and ‖f(A) - f(N)‖²\n    def __init__(self, **kwargs):\n        super().__init__(**kwargs)\n\n    def call(self, anchor, positive, negative):\n        ap_distance = tf.reduce_sum(tf.square(anchor - positive), -1)\n        an_distance = tf.reduce_sum(tf.square(anchor - negative), -1)\n        return (ap_distance, an_distance)\n    \n\ndef get_siamese_network(input_shape = (size, size, 3)):\n    encoder = get_encoder(input_shape)\n    \n    # Input Layers for the images\n    anchor_input   = layers.Input(input_shape, name=\"Anchor_Input\")\n    positive_input = layers.Input(input_shape, name=\"Positive_Input\")\n    negative_input = layers.Input(input_shape, name=\"Negative_Input\")\n    \n    ## Generate the encodings (feature vectors) for the images\n    encoded_a = encoder(anchor_input)\n    encoded_p = encoder(positive_input)\n    encoded_n = encoder(negative_input)\n    \n    # A layer to compute ‖f(A) - f(P)‖² and ‖f(A) - f(N)‖²\n    distances = DistanceLayer()(\n        encoder(anchor_input),\n        encoder(positive_input),\n        encoder(negative_input)\n    )\n    \n    # Creating the Model\n    siamese_network = Model(\n        inputs  = [anchor_input, positive_input, negative_input],\n        outputs = distances,\n        name = \"Siamese_Network\"\n    )\n    return siamese_network,encoder\n\nsiamese_network ,encoder = get_siamese_network()\nsiamese_network.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-09T05:39:53.844689Z","iopub.execute_input":"2024-03-09T05:39:53.845584Z","iopub.status.idle":"2024-03-09T05:40:00.541929Z","shell.execute_reply.started":"2024-03-09T05:39:53.845551Z","shell.execute_reply":"2024-03-09T05:40:00.540911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SiameseModel(Model):\n    # Builds a Siamese model based on a base-model\n    def __init__(self, siamese_network, margin=1.0):\n        super(SiameseModel, self).__init__()\n        \n        self.margin = margin\n        self.siamese_network = siamese_network\n        self.loss_tracker = metrics.Mean(name=\"loss\")\n\n    def call(self, inputs):\n        return self.siamese_network(inputs)\n\n    def train_step(self, data):\n        # GradientTape get the gradients when we compute loss, and uses them to update the weights\n        with tf.GradientTape() as tape:\n            loss = self._compute_loss(data)\n            \n        gradients = tape.gradient(loss, self.siamese_network.trainable_weights)\n        self.optimizer.apply_gradients(zip(gradients, self.siamese_network.trainable_weights))\n        \n        self.loss_tracker.update_state(loss)\n        return {\"loss\": self.loss_tracker.result()}\n\n    def test_step(self, data):\n        loss = self._compute_loss(data)\n        \n        self.loss_tracker.update_state(loss)\n        return {\"loss\": self.loss_tracker.result()}\n\n    def _compute_loss(self, data):\n        # Get the two distances from the network, then compute the triplet loss\n        ap_distance, an_distance = self.siamese_network(data)\n        loss = tf.maximum(ap_distance - an_distance + self.margin, 0.0)\n        return loss\n\n    @property\n    def metrics(self):\n        # We need to list our metrics so the reset_states() can be called automatically.\n        return [self.loss_tracker]","metadata":{"execution":{"iopub.status.busy":"2024-03-08T13:49:42.011261Z","iopub.execute_input":"2024-03-08T13:49:42.011659Z","iopub.status.idle":"2024-03-08T13:49:42.02043Z","shell.execute_reply.started":"2024-03-08T13:49:42.011626Z","shell.execute_reply":"2024-03-08T13:49:42.019395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"siamese_model = SiameseModel(siamese_network)\n\noptimizer = Adam(learning_rate=1e-3, epsilon=1e-01)\nsiamese_model.compile(optimizer=optimizer)  \n#siamese_model.save_model(\"Best_model_params.h5\")\n#siamese_model.load_weights(\"/kaggle/input/trainface5/siamese_model_params\")","metadata":{"execution":{"iopub.status.busy":"2024-03-08T13:49:46.008092Z","iopub.execute_input":"2024-03-08T13:49:46.008487Z","iopub.status.idle":"2024-03-08T13:49:46.031821Z","shell.execute_reply.started":"2024-03-08T13:49:46.008444Z","shell.execute_reply":"2024-03-08T13:49:46.030969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(99)\nnp.random.seed(99)\ntf.random.set_seed(99)\ndef get_allbatch(triplet_list,readhalf=False,batch_size=256):\n    batch_steps = len(triplet_list)//batch_size\n    if readhalf:\n        batch_steps = int(batch_steps/2.0)        \n    else:\n        batch_steps = int(batch_steps)\n    random.shuffle(triplet_list)\n    anchor   = []\n    positive = []\n    negative = []\n    id1 =[]\n    id2 =[]\n    id3 =[]\n    for i in tqdm(range(batch_steps+1)):\n        \n        j = i*batch_size\n        while j<(i+1)*batch_size and j<len(triplet_list):\n            a, p, n = triplet_list[j]            \n            anchor.append(a[1])\n            #print(anchor)\n            positive.append(p[1])\n            negative.append(n[1])\n            id1.append(a[0])\n            id2.append(p[0])\n            id3.append(n[0])\n            j+=1\n            \n        \n        \n    anchor = np.array(anchor)\n    positive = np.array(positive)\n    negative = np.array(negative)    \n    return     anchor,    positive ,    negative ,    id1 ,    id2 ,    id3","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nanchor,    positive ,    negative ,    id1 ,    id2 ,    id3=get_allbatch(train_triplet, batch_size=256)\n\ntrain = pd.DataFrame(anchor, columns=['anchor'])\ntrain['pos'] = positive\ntrain['neg'] = negative\ntrain['id1'] = id1\ntrain['id2'] = id2\ntrain['id3'] = id3\n\nanchor2,    positive2 ,    negative2 ,    id12 ,    id22 ,    id32=get_allbatch(test_triplet,readhalf= False,batch_size=256)\n\ntest = pd.DataFrame(anchor2, columns=['anchor'])\ntest['pos'] = positive2\ntest['neg'] = negative2\ntest['id1'] = id12\ntest['id2'] = id22\ntest['id3'] = id32","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nclass TripleGenerator(tf.keras.utils.Sequence):\n    \n    def __init__(self, gen1, gen2, gen3):\n        \n        self.gen1 = gen1\n        self.gen2 = gen2\n        self.gen3 = gen3\n\n    def __len__(self):\n        \n        return len(self.gen1)\n\n    def __getitem__(self, i):\n        \n        x1 = self.gen1[i]\n        x2 = self.gen2[i]\n        x3 = self.gen3[i]\n        \n        return [x1,x2,x3]\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#directory = '/kaggle/input/celeba-face-recognition-triplets/CelebA FR Triplets/CelebA FR Triplets/images'\n\n\n    #procdirtrain = '/kaggle/tmp/casiawebface-BGR-80x80/'\nprocdirtrain =  '/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/faces-ms1m-refine-v2-80x80/'\nprocdirtrain =  '/kaggle/tmp/faces-ms1m-refine-v2-80x80/' \n#procdirtrain =  '/kaggle/tmp/faces-ms1m-refine-v2-80x80_test/'  \nimg_size = 80\nbatch_size  = 192\n#/kaggle/input/celeba-face-recognition-triplets/'CelebA FR Triplets'/'CelebA FR Triplets'/images\n#统一用BGR顺序：在ImageDataGenerator中传入preprocessing_function，把RGB的转换为BGR\n\n#def img_rgb2bgr(img):\n#    return img[: , : , : : -1]\n\n\n#统一用RGB顺序：在OpenCV读取图像文件时，把BGR的转换为RGB\n\n#import cv2\n#from PIL import Image\n\n#img = cv2.imread(\"plane.jpg\")\n#image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n\ndatagen = ImageDataGenerator(rescale=1./255,                             \n                            #rotation_range =6,    \n                            #zoom_range = 0.2,\n                            horizontal_flip=True)\ntestdatagen = ImageDataGenerator(rescale=1./255)                           \n                            #rotation_range =3, \n                            \n                            #horizontal_flip=True)\ndef create_generator(folder,dataset,column,datagen):\n    \n    generator = datagen.flow_from_dataframe(dataframe=dataset,\n                                            directory=folder,\n                                            x_col=column,\n                                            target_size=(img_size, img_size),\n                                            batch_size=batch_size,\n                                            class_mode=None,\n                                            shuffle=False)\n    return generator\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"procdirtrain = '/kaggle/input/faces-ms1m-refine-v2-80x80-bgr/kaggle/tmp/faces-ms1m-refine-v2-80x80/'\nprocdirtrain =  '/kaggle/tmp/faces-ms1m-refine-v2-80x80/' \n#procdirtrain =  '/kaggle/tmp/faces-ms1m-refine-v2-80x80_test/'  \ntrain_generator1 = create_generator(procdirtrain,train,'anchor',datagen)\ntrain_generator2 = create_generator(procdirtrain,train,'pos',datagen)\ntrain_generator3 = create_generator(procdirtrain,train,'neg',datagen)\n\ntest_generator1 = create_generator(procdirtrain,test,'anchor',testdatagen)\ntest_generator2 = create_generator(procdirtrain,test,'pos',testdatagen)\ntest_generator3 = create_generator(procdirtrain,test,'neg',testdatagen)\n\ntrain_generator = TripleGenerator(train_generator1,train_generator2,train_generator3)\ntest_generator = TripleGenerator(test_generator1,test_generator2,test_generator3)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples = train_generator[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_on_train(samples,batch_size = 256):\n    pos_scores, neg_scores = [], []\n\n    for data in samples:\n        prediction = siamese_model.predict(data)\n        pos_scores += list(prediction[0])\n        neg_scores += list(prediction[1])\n    \n    accuracy = np.sum(np.array(pos_scores) < np.array(neg_scores)) / len(pos_scores)\n    ap_mean = np.mean(pos_scores)\n    an_mean = np.mean(neg_scores)\n    ap_stds = np.std(pos_scores)\n    an_stds = np.std(neg_scores)\n    \n    print(f\"Accuracy on test = {accuracy:.5f}\")\n    return (accuracy, ap_mean, an_mean, ap_stds, an_stds)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, axarr = plt.subplots(15,3,figsize=(20, 60))\n\nfor i in range(0,15):\n\n    axarr[i,0].imshow(samples[0][i])\n    axarr[i,0].title.set_text('Anchor Image')\n    axarr[i,0].axis('off')\n    axarr[i,1].imshow(samples[1][i])\n    axarr[i,1].title.set_text('Positive Image')\n    axarr[i,1].axis('off')\n    axarr[i,2].imshow(samples[2][i])\n    axarr[i,2].title.set_text('Negative Image')\n    axarr[i,2].axis('off')\n\nf, axarr = plt.subplots(15,3,figsize=(20, 60))\n\nfor i in range(0,15):\n\n    axarr[i,0].imshow(samples[0][i+16])\n    axarr[i,0].title.set_text('Anchor Image')\n    axarr[i,0].axis('off')\n    axarr[i,1].imshow(samples[1][i+16])\n    axarr[i,1].title.set_text('Positive Image')\n    axarr[i,1].axis('off')\n    axarr[i,2].imshow(samples[2][i+16])\n    axarr[i,2].title.set_text('Negative Image')\n    axarr[i,2].axis('off')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_case(batch_size = 256):\n    pos_scores, neg_scores = [], []\n    count = 0\n    resu = 0\n    for t1 in range(0,12):        \n        samples= test_generator[t1]\n        pos_scores,neg_scores = siamese_model(samples)\n            #print(pos_dist)\n            #print(neg_dist)\n        accuracy = np.sum(np.array(pos_scores) < np.array(neg_scores)) / len(neg_scores)\n        ap_mean = np.mean(pos_scores)\n        an_mean = np.mean(neg_scores)\n        ap_stds = np.std(pos_scores)\n        an_stds = np.std(neg_scores)\n        count = count+1\n        resu = resu + accuracy        \n        print(f\"Accuracy on test = {accuracy:.5f}\")\n    resu = resu/count\n    print(f\"Accuracy on avg test = {resu:.5f}\")\n    return (accuracy, ap_mean, an_mean, ap_stds, an_stds,resu)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import losses\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras import metrics\n#siamese_model2 = SiameseModel(siamese_network)\nsiamese_model.compile(optimizer=optimizers.Adam(learning_rate=0.0001))","metadata":{"execution":{"iopub.status.busy":"2024-03-08T13:50:01.836292Z","iopub.execute_input":"2024-03-08T13:50:01.836678Z","iopub.status.idle":"2024-03-08T13:50:01.848953Z","shell.execute_reply.started":"2024-03-08T13:50:01.836647Z","shell.execute_reply":"2024-03-08T13:50:01.848008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50, VGG16, InceptionV3, MobileNetV2, EfficientNetB3\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n#siamese_model.load_weights(\"/kaggle/input/trainface2/siamese_model-final\")\nmodel_path = \"model.h5\"\ncheckpoint = ModelCheckpoint(model_path,\n                             monitor=\"val_loss\",\n                             mode=\"min\",\n                             save_best_only = True,\n                             verbose=1,\n                             save_weights_only=True)\n\nearlystop = EarlyStopping(monitor = 'val_loss', \n                          min_delta = 0, \n                          patience = 5,\n                          verbose = 1,\n                          restore_best_weights = True)\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', \n                                            patience=4, \n                                            verbose=1, \n                                            factor=0.3, \n                                            min_lr=0.00000001)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"siamese_model.load_weights(\"/kaggle/input/ms1mtraindata2/model.h5\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#loss: 0.2054  Accuracy on avg test = 0.94180\n#loss: 0.1560  Accuracy on avg test = 0.95176 val_loss: 0.1792\n#loss: 0.1605 - val_loss: 0.1664\n#loss: 0.1551 - val_loss: 0.1711 Accuracy on avg test = 0.95605   Accuracy on avg data= 0.96734\nhistory = siamese_model.fit(train_generator, validation_data=test_generator,shuffle=True,\n                            epochs=1,callbacks=[checkpoint,earlystop,learning_rate_reduction]) \ntest_case()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_all(test_generator,size):\n    pos_scores, neg_scores = [], []\n    count = 0\n    resu = 0\n    samples= test_generator[1]\n    lsize = size\n    for t1 in range(0,lsize):        \n        samples= test_generator[t1]\n        pos_scores,neg_scores = siamese_model(samples)\n            #print(pos_dist)\n            #print(neg_dist)\n        accuracy = np.sum(np.array(pos_scores) < np.array(neg_scores)) / len(neg_scores)\n        ap_mean = np.mean(pos_scores)\n        an_mean = np.mean(neg_scores)\n        ap_stds = np.std(pos_scores)\n        an_stds = np.std(neg_scores)\n        count = count+1\n        resu = resu + accuracy        \n        print(f\"Accuracy on test = {accuracy:.5f}\")\n    resu = resu/count\n    print(f\"Accuracy on avg test = {resu:.5f}\")\n    return (accuracy, ap_mean, an_mean, ap_stds, an_stds,resu)\ntest_all(test_generator,80)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_all(train_generator,200)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"tpu train code ","metadata":{}},{"cell_type":"code","source":"# Import standard dependencies\nimport os\nimport math\nimport cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport PIL\nfrom sklearn.preprocessing import normalize # for normalizing np array to avoid NaN from dividing by 0\nimport glob\n\n# Import tensorflow dependencies - Functional API\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input, Dropout, BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2024-03-31T05:50:18.483683Z","iopub.execute_input":"2024-03-31T05:50:18.484037Z","iopub.status.idle":"2024-03-31T05:50:35.215493Z","shell.execute_reply.started":"2024-03-31T05:50:18.484006Z","shell.execute_reply":"2024-03-31T05:50:35.214467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nstrategy = tf.distribute.MirroredStrategy() # for GPU or multi-GPU machines","metadata":{"execution":{"iopub.status.busy":"2024-03-17T06:06:03.370059Z","iopub.execute_input":"2024-03-17T06:06:03.370622Z","iopub.status.idle":"2024-03-17T06:06:07.466723Z","shell.execute_reply.started":"2024-03-17T06:06:03.370589Z","shell.execute_reply":"2024-03-17T06:06:07.465837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\nstrategy = tf.distribute.TPUStrategy(tpu) ","metadata":{"execution":{"iopub.status.busy":"2024-03-31T05:50:35.216945Z","iopub.execute_input":"2024-03-31T05:50:35.217860Z","iopub.status.idle":"2024-03-31T05:50:43.992953Z","shell.execute_reply.started":"2024-03-31T05:50:35.217822Z","shell.execute_reply":"2024-03-31T05:50:43.992131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 85742\ntotal_examples = 5822653","metadata":{"execution":{"iopub.status.busy":"2024-03-31T05:50:47.215000Z","iopub.execute_input":"2024-03-31T05:50:47.215404Z","iopub.status.idle":"2024-03-31T05:50:47.219889Z","shell.execute_reply.started":"2024-03-31T05:50:47.215374Z","shell.execute_reply":"2024-03-31T05:50:47.219089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = tf.data.TFRecordDataset.list_files(\"/kaggle/input/faces-ms1m-refine-v2-112x112-tfrecord/faces_ms1m_refine_v2_112x112-*.tfrecord\")\ntrain_ds = tf.data.TFRecordDataset(filenames, num_parallel_reads = tf.data.AUTOTUNE)\n\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = {'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}","metadata":{"execution":{"iopub.status.busy":"2024-03-31T06:15:01.612633Z","iopub.execute_input":"2024-03-31T06:15:01.613504Z","iopub.status.idle":"2024-03-31T06:15:01.660150Z","shell.execute_reply.started":"2024-03-31T06:15:01.613468Z","shell.execute_reply":"2024-03-31T06:15:01.659128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_tfrecord_fn(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3))     \n    img = tf.image.resize(img,[60,60])\n    img = tf.image.resize(img,[112,112])\n    img = tf.cast(img, dtype=tf.float32)\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    #img = img / 127.5 - 1.0     # Range is [-1, 1]\n    img = img /255.0\n    img = tf.image.random_flip_left_right(img)\n    tf.image.random_jpeg_quality(img,30,70)\n    label = tf.cast(example['label'], dtype=tf.int32)\n    return img, label","metadata":{"execution":{"iopub.status.busy":"2024-03-18T05:33:14.107029Z","iopub.execute_input":"2024-03-18T05:33:14.107913Z","iopub.status.idle":"2024-03-18T05:33:14.113655Z","shell.execute_reply.started":"2024-03-18T05:33:14.10787Z","shell.execute_reply":"2024-03-18T05:33:14.11269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nrandom.seed(5)\nnp.random.seed(5)\ntf.random.set_seed(5)\n\nTRAINING_FILENAMES = []\nVALIDATION_FILENAMES =[]\ndatadir = '/kaggle/input/faces-ms1m-refine-v2-112x112-tfrecord/'\n\ntrcount = 0\nfor filename in os.listdir(datadir):\n    path = os.path.join(datadir,filename)\n    if (filename=='lfw.bin'):\n        continue\n    if (trcount<=12):\n        if not os.path.isdir(path):\n            TRAINING_FILENAMES.append(path)\n        trcount = trcount+1\n        print(path)\n    else:\n        if not os.path.isdir(path):\n            VALIDATION_FILENAMES.append(path)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T06:07:21.328735Z","iopub.execute_input":"2024-03-17T06:07:21.32907Z","iopub.status.idle":"2024-03-17T06:07:21.349702Z","shell.execute_reply.started":"2024-03-17T06:07:21.329041Z","shell.execute_reply":"2024-03-17T06:07:21.348608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ignore_order = tf.data.Options()\nignore_order.experimental_deterministic = False\n     \ntrain_ds = tf.data.TFRecordDataset(TRAINING_FILENAMES, num_parallel_reads = tf.data.AUTOTUNE)\ntrain_ds = train_ds.with_options(ignore_order)\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = {'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}","metadata":{"execution":{"iopub.status.busy":"2024-03-31T06:15:13.163004Z","iopub.execute_input":"2024-03-31T06:15:13.163373Z","iopub.status.idle":"2024-03-31T06:15:13.181510Z","shell.execute_reply.started":"2024-03-31T06:15:13.163342Z","shell.execute_reply":"2024-03-31T06:15:13.180562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ignore_order = tf.data.Options()\nignore_order.experimental_deterministic = False\n     \nval_ds = tf.data.TFRecordDataset(VALIDATION_FILENAMES, num_parallel_reads = tf.data.AUTOTUNE)\nval_ds = train_ds.with_options(ignore_order)\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = {'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}","metadata":{"execution":{"iopub.status.busy":"2024-03-31T06:15:16.536654Z","iopub.execute_input":"2024-03-31T06:15:16.536976Z","iopub.status.idle":"2024-03-31T06:15:16.555367Z","shell.execute_reply.started":"2024-03-31T06:15:16.536947Z","shell.execute_reply":"2024-03-31T06:15:16.554331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn,num_parallel_calls = tf.data.AUTOTUNE)\nval_ds = val_ds.map(parse_tfrecord_fn,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T06:08:38.423355Z","iopub.execute_input":"2024-03-17T06:08:38.42376Z","iopub.status.idle":"2024-03-17T06:08:38.59821Z","shell.execute_reply.started":"2024-03-17T06:08:38.423729Z","shell.execute_reply":"2024-03-17T06:08:38.597024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img,label in val_ds.take(3):\n  print(label)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T06:08:40.850479Z","iopub.execute_input":"2024-03-17T06:08:40.850908Z","iopub.status.idle":"2024-03-17T06:08:40.922177Z","shell.execute_reply.started":"2024-03-17T06:08:40.850872Z","shell.execute_reply":"2024-03-17T06:08:40.921069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nplt.axis('off')\nfor idx, (image, label) in enumerate(val_ds.take(6)):\n\n    ax = plt.subplot(2, 3, idx + 1)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    \n    # Plot the image\n    plt.imshow(image)\n    plt.title(f\"Label: {label}\")\n    plt.axis('off')\nplt.tight_layout()\n# plt.subplots_adjust(wspace=0.1, hspace=0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T06:08:48.735766Z","iopub.execute_input":"2024-03-17T06:08:48.736175Z","iopub.status.idle":"2024-03-17T06:08:49.315054Z","shell.execute_reply.started":"2024-03-17T06:08:48.736144Z","shell.execute_reply":"2024-03-17T06:08:49.313843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ver 1.00 begin","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\n\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\nstrategy = tf.distribute.TPUStrategy(tpu) ","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:01:21.183826Z","iopub.execute_input":"2024-04-03T03:01:21.184209Z","iopub.status.idle":"2024-04-03T03:01:36.385290Z","shell.execute_reply.started":"2024-04-03T03:01:21.184178Z","shell.execute_reply":"2024-04-03T03:01:36.384481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GPU\nimport tensorflow as tf\nstrategy = tf.distribute.MirroredStrategy() # for GPU or multi-GPU machines","metadata":{"execution":{"iopub.status.busy":"2024-03-19T13:17:52.884127Z","iopub.execute_input":"2024-03-19T13:17:52.88465Z","iopub.status.idle":"2024-03-19T13:17:53.75364Z","shell.execute_reply.started":"2024-03-19T13:17:52.884623Z","shell.execute_reply":"2024-03-19T13:17:53.752447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:01:40.829312Z","iopub.execute_input":"2024-04-03T03:01:40.829629Z","iopub.status.idle":"2024-04-03T03:01:40.833924Z","shell.execute_reply.started":"2024-04-03T03:01:40.829602Z","shell.execute_reply":"2024-04-03T03:01:40.833178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import standard dependencies\nimport os\nimport math\nimport cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport PIL\nfrom sklearn.preprocessing import normalize # for normalizing np array to avoid NaN from dividing by 0\nimport glob\n\n# Import tensorflow dependencies - Functional API\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input, Dropout, BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:01:43.601023Z","iopub.execute_input":"2024-04-03T03:01:43.601363Z","iopub.status.idle":"2024-04-03T03:01:50.773961Z","shell.execute_reply.started":"2024-04-03T03:01:43.601333Z","shell.execute_reply":"2024-04-03T03:01:50.773008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\ny = np.array([0,1,4,2,3])\nonehot = to_categorical(y,num_classes=6)\nprint(onehot)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:01:50.775405Z","iopub.execute_input":"2024-04-03T03:01:50.775912Z","iopub.status.idle":"2024-04-03T03:01:50.780753Z","shell.execute_reply.started":"2024-04-03T03:01:50.775880Z","shell.execute_reply":"2024-04-03T03:01:50.780077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 85742\ntotal_examples = 5822653","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:01:53.036824Z","iopub.execute_input":"2024-04-03T03:01:53.037112Z","iopub.status.idle":"2024-04-03T03:01:53.183892Z","shell.execute_reply.started":"2024-04-03T03:01:53.037084Z","shell.execute_reply":"2024-04-03T03:01:53.183052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nrandom.seed(5)\nnp.random.seed(5)\ntf.random.set_seed(5)\n\nTRAINING_FILENAMES = []\nVALIDATION_FILENAMES =[]\ndatadir = '/kaggle/input/faces-ms1m-refine-v2-112x112-tfrecord/'\n\ntrcount = 0\nfor filename in os.listdir(datadir):\n    path = os.path.join(datadir,filename)\n    if (filename=='lfw.bin'):\n        continue\n    if (trcount<=12):\n        if not os.path.isdir(path):\n            TRAINING_FILENAMES.append(path)\n        trcount = trcount+1\n        print(path)\n    else:\n        if not os.path.isdir(path):\n            VALIDATION_FILENAMES.append(path)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:01:55.175005Z","iopub.execute_input":"2024-04-03T03:01:55.175737Z","iopub.status.idle":"2024-04-03T03:01:55.188314Z","shell.execute_reply.started":"2024-04-03T03:01:55.175699Z","shell.execute_reply":"2024-04-03T03:01:55.187558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from numpy import save\nsave('TRAINING_FILENAMES.npy',TRAINING_FILENAMES)\nsave('VALIDATION_FILENAMES.npy',VALIDATION_FILENAMES)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-18T01:28:58.824901Z","iopub.execute_input":"2024-03-18T01:28:58.825829Z","iopub.status.idle":"2024-03-18T01:28:58.831444Z","shell.execute_reply.started":"2024-03-18T01:28:58.825785Z","shell.execute_reply":"2024-03-18T01:28:58.830336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom numpy import load\n\nTRAINING_FILENAMES = load('/kaggle/input/tputraindata6/TRAINING_FILENAMES.npy')\nVALIDATION_FILENAMES= load('/kaggle/input/tputraindata6/VALIDATION_FILENAMES.npy')","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:01:59.988095Z","iopub.execute_input":"2024-04-03T03:01:59.988420Z","iopub.status.idle":"2024-04-03T03:02:03.110380Z","shell.execute_reply.started":"2024-04-03T03:01:59.988393Z","shell.execute_reply":"2024-04-03T03:02:03.109528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(TRAINING_FILENAMES)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:02:05.409442Z","iopub.execute_input":"2024-04-03T03:02:05.409970Z","iopub.status.idle":"2024-04-03T03:02:05.414054Z","shell.execute_reply.started":"2024-04-03T03:02:05.409935Z","shell.execute_reply":"2024-04-03T03:02:05.413206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(VALIDATION_FILENAMES)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:02:09.303420Z","iopub.execute_input":"2024-04-03T03:02:09.303784Z","iopub.status.idle":"2024-04-03T03:02:14.688192Z","shell.execute_reply.started":"2024-04-03T03:02:09.303745Z","shell.execute_reply":"2024-04-03T03:02:14.687245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ignore_order = tf.data.Options()\nignore_order.experimental_deterministic = False\n     \ntrain_ds = tf.data.TFRecordDataset(TRAINING_FILENAMES, num_parallel_reads = tf.data.AUTOTUNE)\ntrain_ds = train_ds.with_options(ignore_order)\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = {'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:10:37.984563Z","iopub.execute_input":"2024-04-03T03:10:37.985008Z","iopub.status.idle":"2024-04-03T03:10:38.000928Z","shell.execute_reply.started":"2024-04-03T03:10:37.984942Z","shell.execute_reply":"2024-04-03T03:10:38.000038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ignore_order = tf.data.Options()\nignore_order.experimental_deterministic = False\n     \nval_ds = tf.data.TFRecordDataset(VALIDATION_FILENAMES, num_parallel_reads = tf.data.AUTOTUNE)\nval_ds = train_ds.with_options(ignore_order)\n# Raw_dataset contains serialized tf.train.Example messages, so we need to parse it.\n# Create a description of the features.\nfeature_description = {'image_raw': tf.io.FixedLenFeature([], tf.string),\n                        'label': tf.io.FixedLenFeature([], tf.int64)}","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:10:40.847199Z","iopub.execute_input":"2024-04-03T03:10:40.847511Z","iopub.status.idle":"2024-04-03T03:10:40.863048Z","shell.execute_reply.started":"2024-04-03T03:10:40.847484Z","shell.execute_reply":"2024-04-03T03:10:40.862133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_tfrecord_fn_2(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.cast(img, dtype=tf.float32)\n    img = tf.image.resize(img,[28,28])\n    #img = tf.image.resize(img,[112,112])\n    img = tf.image.resize(img,[80,80])\n    img = tf.image.random_flip_left_right(img)\n    tf.image.random_jpeg_quality(img,30,70)\n    img = img / 255.0\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)\n    return img, label","metadata":{"execution":{"iopub.status.busy":"2024-04-03T02:56:07.861238Z","iopub.execute_input":"2024-04-03T02:56:07.861968Z","iopub.status.idle":"2024-04-03T02:56:07.867908Z","shell.execute_reply.started":"2024-04-03T02:56:07.861927Z","shell.execute_reply":"2024-04-03T02:56:07.866824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn_2,num_parallel_calls = tf.data.AUTOTUNE)\nval_ds = val_ds.map(parse_tfrecord_fn_2,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:21:37.549104Z","iopub.execute_input":"2024-04-02T03:21:37.549468Z","iopub.status.idle":"2024-04-02T03:21:37.637900Z","shell.execute_reply.started":"2024-04-02T03:21:37.549437Z","shell.execute_reply":"2024-04-02T03:21:37.636641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_tfrecord_fn_3(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.cast(img, dtype=tf.float32)\n    img = tf.image.resize(img,[32,32])\n    img = tf.image.resize(img,[112,112])\n    #img = tf.image.resize(img,[80,80])\n    img = tf.image.random_flip_left_right(img)\n    tf.image.random_jpeg_quality(img,30,60)\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)\n    return img, label","metadata":{"execution":{"iopub.status.busy":"2024-03-25T04:48:14.48978Z","iopub.execute_input":"2024-03-25T04:48:14.490182Z","iopub.status.idle":"2024-03-25T04:48:14.497072Z","shell.execute_reply.started":"2024-03-25T04:48:14.490148Z","shell.execute_reply":"2024-03-25T04:48:14.495863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn_3,num_parallel_calls = tf.data.AUTOTUNE)\nval_ds = val_ds.map(parse_tfrecord_fn_3,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-03-25T04:48:17.158222Z","iopub.execute_input":"2024-03-25T04:48:17.158553Z","iopub.status.idle":"2024-03-25T04:48:17.250593Z","shell.execute_reply.started":"2024-03-25T04:48:17.158524Z","shell.execute_reply":"2024-03-25T04:48:17.249387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_tfrecord_fn_6(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.cast(img, dtype=tf.float32)\n    img = tf.image.random_flip_left_right(img)\n    #imagetarget = tf.image.resize(img,[80,80])\n    #imagetarget = imagetarget / 255.0\n    img = tf.image.resize(img,[12,12])\n    #img = tf.image.resize(img,[112,112])\n    img = tf.image.resize(img,[40,40])\n    \n    tf.image.random_jpeg_quality(img,30,70)\n    img = img / 255.0\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)\n    #imgtarget = [imagetarget,label]\n    return img,label \n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-02T07:19:23.341793Z","iopub.execute_input":"2024-04-02T07:19:23.342208Z","iopub.status.idle":"2024-04-02T07:19:23.349701Z","shell.execute_reply.started":"2024-04-02T07:19:23.342172Z","shell.execute_reply":"2024-04-02T07:19:23.348617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn_6,num_parallel_calls = tf.data.AUTOTUNE)\nval_ds = val_ds.map(parse_tfrecord_fn_6,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_tfrecord_fn(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.cast(img, dtype=tf.float32)\n    img = tf.image.resize(img,[50,50])\n    img = tf.image.resize(img,[160,160])\n    #img = tf.image.resize(img,[80,80])\n    img = tf.image.random_flip_left_right(img)\n    tf.image.random_jpeg_quality(img,30,70)\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)\n    return img, label","metadata":{"execution":{"iopub.status.busy":"2024-03-24T01:30:39.145458Z","iopub.execute_input":"2024-03-24T01:30:39.146226Z","iopub.status.idle":"2024-03-24T01:30:39.151968Z","shell.execute_reply.started":"2024-03-24T01:30:39.146186Z","shell.execute_reply":"2024-03-24T01:30:39.151074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn,num_parallel_calls = tf.data.AUTOTUNE)\nval_ds = val_ds.map(parse_tfrecord_fn,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T01:30:41.057751Z","iopub.execute_input":"2024-03-24T01:30:41.058511Z","iopub.status.idle":"2024-03-24T01:30:41.212561Z","shell.execute_reply.started":"2024-03-24T01:30:41.058478Z","shell.execute_reply":"2024-03-24T01:30:41.211784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#12x12 to 80x80\ndef parse_tfrecord_fn_9(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.cast(img, dtype=tf.float32)\n    img = tf.image.random_flip_left_right(img)\n    imgTarget = tf.image.resize(img,[80,80])\n    imgTarget = imgTarget/255.0\n    img = tf.image.resize(img,[12,12])\n    #img = tf.image.resize(img,[112,112])\n    img = tf.image.resize(img,[40,40])\n    #img = tf.image.random_flip_left_right(img)\n    tf.image.random_jpeg_quality(img,30,60)\n    img = img / 255.0\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)\n    return img, imgTarget","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:10:47.362676Z","iopub.execute_input":"2024-04-03T03:10:47.363031Z","iopub.status.idle":"2024-04-03T03:10:47.369191Z","shell.execute_reply.started":"2024-04-03T03:10:47.363001Z","shell.execute_reply":"2024-04-03T03:10:47.368378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn_9,num_parallel_calls = tf.data.AUTOTUNE)\nval_ds = val_ds.map(parse_tfrecord_fn_9,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:10:52.007801Z","iopub.execute_input":"2024-04-03T03:10:52.008639Z","iopub.status.idle":"2024-04-03T03:10:52.097672Z","shell.execute_reply.started":"2024-04-03T03:10:52.008599Z","shell.execute_reply":"2024-04-03T03:10:52.096893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_tfrecord_fn_80x80(example):\n    example = tf.io.parse_single_example(example, feature_description)\n    img = tf.io.decode_jpeg(example[\"image_raw\"])\n    img = tf.reshape(img, shape=(112, 112, 3)) \n    img = tf.cast(img, dtype=tf.float32)\n    #img = tf.image.resize(img,[60,60])\n    #img = tf.image.resize(img,[112,112])\n    img = tf.image.resize(img,[80,80])\n    img = tf.image.random_flip_left_right(img)\n    tf.image.random_jpeg_quality(img,30,60)\n    #img = tf.image.resize(img,[160,160])\n    #img = tf.image.resize(img,[80,80])\n    # For EfficientNetV2, by default input preprocessing is included as a part of the model \n    # (as a Rescaling layer)\n    # img = img / 127.5 - 1.0     # Range is [-1, 1]\n    label = tf.cast(example['label'], dtype=tf.int32)\n    return img, label","metadata":{"execution":{"iopub.status.busy":"2024-03-24T04:49:33.248852Z","iopub.execute_input":"2024-03-24T04:49:33.249651Z","iopub.status.idle":"2024-03-24T04:49:33.256004Z","shell.execute_reply.started":"2024-03-24T04:49:33.249613Z","shell.execute_reply":"2024-03-24T04:49:33.255103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(parse_tfrecord_fn_80x80,num_parallel_calls = tf.data.AUTOTUNE)\nval_ds = val_ds.map(parse_tfrecord_fn_80x80,num_parallel_calls = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T04:49:36.535388Z","iopub.execute_input":"2024-03-24T04:49:36.536101Z","iopub.status.idle":"2024-03-24T04:49:36.68652Z","shell.execute_reply.started":"2024-03-24T04:49:36.536063Z","shell.execute_reply":"2024-03-24T04:49:36.685285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img,label in val_ds.take(3):\n  print(label)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelpredict = modevgg16_gen_20_7\nplt.figure(figsize=(32, 32))\nplt.axis('off')\nfor idx, (image, imgTarget) in enumerate(val_ds.take(9)):\n    predict = modelpredict.predict(image)\n    predict = np.clip(predict, 0., 1.)\n    break;\ndef printResu(image,predict,imgTarget,addi):\n    plt.figure(figsize=(32, 32))\n    plt.axis('off')\n    for i in range(0,9):\n    \n        ax = plt.subplot(9, 3, i*3 + 1)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n        imgsource = image[i+addi] #/ 255.0\n        print(image.shape)\n    # Plot the image\n        plt.imshow(imgsource)\n        plt.title(f\"source: 40x40\")\n        plt.axis('off')\n    \n        ax = plt.subplot(9, 3, i*3 + 2)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n        pred = predict[i+addi] \n        #pred = pred / 255.0\n    \n    # Plot the image\n        plt.imshow(pred)\n        plt.title(f\"predict: 80x80\")\n        plt.axis('off')\n    \n        ax = plt.subplot(9, 3, i*3 + 3)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #imgTarget = imgTarget / 255.0\n        print(imgTarget.shape)\n        imgT = imgTarget[i+addi] # / 255.0\n    # Plot the image\n        plt.imshow(imgT)\n        plt.title(f\"Target: 80x80\")\n        plt.axis('off')\n    plt.tight_layout()\n# plt.subplots_adjust(wspace=0.1, hspace=0.1)\n    plt.show()\nprintResu(image,predict,imgTarget,0)\nprintResu(image,predict,imgTarget,27)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting first 6 examples\n#12x12 to 80x80\nplt.figure(figsize=(9, 9))\nplt.axis('off')\nfor idx, (image, label) in enumerate(val_ds.take(18)):\n\n    ax = plt.subplot(6, 3, idx + 1)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    print(image.shape)\n    # Plot the image\n    plt.imshow(image)\n    #plt.title(f\"Label: {label}\")\n    plt.axis('off')\nplt.tight_layout()\n# plt.subplots_adjust(wspace=0.1, hspace=0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:02:54.653031Z","iopub.execute_input":"2024-04-03T03:02:54.653349Z","iopub.status.idle":"2024-04-03T03:02:59.192573Z","shell.execute_reply.started":"2024-04-03T03:02:54.653322Z","shell.execute_reply":"2024-04-03T03:02:59.191757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting first 6 examples\n\nplt.figure(figsize=(9, 9))\nplt.axis('off')\nfor idx, (image, label) in enumerate(val_ds.take(18)):\n\n    ax = plt.subplot(6, 3, idx + 1)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    print(image.shape)\n    # Plot the image\n    plt.imshow(image)\n    plt.title(f\"Label: {label}\")\n    plt.axis('off')\nplt.tight_layout()\n# plt.subplots_adjust(wspace=0.1, hspace=0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:21:46.195789Z","iopub.execute_input":"2024-04-02T03:21:46.196159Z","iopub.status.idle":"2024-04-02T03:21:47.231149Z","shell.execute_reply.started":"2024-04-02T03:21:46.196127Z","shell.execute_reply":"2024-04-02T03:21:47.229905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelpredict = modevgg16_gen_20_7\nplt.figure(figsize=(32, 32))\nplt.axis('off')\nfor idx, (image, label) in enumerate(val_ds.take(9)):\n    predict = modelpredict.predict(image)\n    predict = np.clip(predict, 0., 1.)\n    break;\ndef printResu(image,predict,addi):\n    plt.figure(figsize=(32, 32))\n    plt.axis('off')\n    for i in range(0,9):\n    \n        ax = plt.subplot(9, 2, i*2 + 1)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n        imgsource = image[i+addi] #/ 255.0\n        print(image.shape)\n    # Plot the image\n        plt.imshow(imgsource)\n        plt.title(f\"source: 40x40\")\n        plt.axis('off')\n    \n        ax = plt.subplot(9, 2, i*2 + 2)\n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n        pred = predict[i+addi] \n        #pred = pred / 255.0\n        print(pred.shape)\n    # Plot the image\n        plt.imshow(pred)\n        plt.title(f\"predict: 80x80\")\n        plt.axis('off')\n    \n        \n    plt.tight_layout()\n# plt.subplots_adjust(wspace=0.1, hspace=0.1)\n    plt.show()\nprintResu(image,predict,0)\nprintResu(image,predict,27)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:12:31.715773Z","iopub.execute_input":"2024-04-03T03:12:31.716438Z","iopub.status.idle":"2024-04-03T03:12:54.127688Z","shell.execute_reply.started":"2024-04-03T03:12:31.716404Z","shell.execute_reply":"2024-04-03T03:12:54.126559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#first run train 3 times 26x26->80X80 val\n\nplt.figure(figsize=(32, 32))\nplt.axis('off')\n\n    \nfor idx, (image, label) in enumerate(val_ds.take(9)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n   \n    cos_theta = CosFaceModel_modi(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#first run train 3 times 12x12->80X80 val\n\nplt.figure(figsize=(32, 32))\nplt.axis('off')\n\n    \nfor idx, (image, label) in enumerate(val_ds.take(9)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n   \n    cos_theta = CosFaceModel_modi(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:01:26.122457Z","iopub.execute_input":"2024-04-02T03:01:26.123615Z","iopub.status.idle":"2024-04-02T03:06:31.469196Z","shell.execute_reply.started":"2024-04-02T03:01:26.123573Z","shell.execute_reply":"2024-04-02T03:06:31.468117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, (image, label) in enumerate(val_ds.take(9)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n   \n    cos_theta = CosFaceModel_modi(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"tf.Tensor(\n[[ 5.46386205e-02  2.67929062e-02  1.06006609e-02 ... -3.61186229e-02\n   3.24105518e-03  1.64163008e-01]\n [-2.00817272e-01 -5.34135215e-02  8.67685750e-02 ... -9.65396613e-02\n   1.10135952e-04 -7.00016469e-02]\n [ 6.80693835e-02  1.43931344e-01 -1.45420060e-01 ... -7.75982440e-02\n   3.29408632e-03  1.17870346e-01]\n ...\n [ 4.22129631e-02  3.65682840e-02  1.29977509e-01 ...  7.15050027e-02\n   4.15223837e-02 -2.06932314e-02]\n [ 3.19554448e-01 -1.10922061e-01 -1.21212415e-02 ... -8.56157318e-02\n   2.03144237e-01  1.80469632e-01]\n [ 1.29938230e-01 -2.60297030e-01 -1.47857383e-01 ... -8.03788230e-02\n  -4.78443131e-02 -7.94401988e-02]], shape=(2048, 85742), dtype=float32) [  904 53224 10299 ... 58489 42530 37701] tf.Tensor([  904 53224 10299 ... 58489  8842 37701], shape=(2048,), dtype=int32)\ncos_theta tf.Tensor(\n[ 0.05463862  0.02679291  0.01060066 ... -0.03611862  0.00324106\n  0.16416301], shape=(85742,), dtype=float32)\nshape (2048,)\n0 ,right: 1839\ntf.Tensor(\n[[-0.18460736 -0.00043249  0.06276699 ...  0.06810557 -0.0895439\n   0.05305077]\n [ 0.06265213 -0.28918016  0.01994701 ... -0.24519362 -0.02657561\n   0.1266872 ]\n [-0.03363685 -0.04328204  0.06448715 ... -0.00210111 -0.04405233\n   0.00317585]\n ...\n [ 0.08550449  0.10536363 -0.09820917 ...  0.16446309 -0.0105176\n  -0.04183441]\n [ 0.04082456 -0.28489247 -0.19053383 ... -0.11284415  0.07786818\n  -0.21574925]\n [-0.13634822  0.01124615  0.10843849 ... -0.09032082 -0.05308701\n  -0.01591271]], shape=(2048, 85742), dtype=float32) [19847 82121 24133 ... 22031  7796 10693] tf.Tensor([19847 82121 24133 ... 22031  7796 10693], shape=(2048,), dtype=int32)\ncos_theta tf.Tensor(\n[-0.18460736 -0.00043249  0.06276699 ...  0.06810557 -0.0895439\n  0.05305077], shape=(85742,), dtype=float32)\nshape (2048,)\n1 ,right: 1831\ntf.Tensor(\n[[ 0.02890988  0.13937466 -0.15421328 ... -0.03214615 -0.02040114\n   0.04279447]\n [ 0.01811897 -0.03756558  0.08683791 ...  0.09450029 -0.08960015\n   0.10066327]\n [ 0.04083549  0.07963559 -0.00346882 ... -0.02534236  0.14899495\n  -0.02679447]\n ...\n [ 0.1128726  -0.1723947  -0.10200793 ...  0.000403    0.02678353\n  -0.15615869]\n [-0.03318568 -0.03511749  0.3190377  ...  0.08866545 -0.11194351\n   0.13040274]\n [-0.06109697 -0.01870938  0.2934582  ... -0.22784439 -0.17202492\n   0.07997974]], shape=(2048, 85742), dtype=float32) [41309 81401  8231 ...  1939  2719 32853] tf.Tensor([41309 81401  8231 ... 75792  2719 32853], shape=(2048,), dtype=int32)\ncos_theta tf.Tensor(\n[ 0.02890988  0.13937466 -0.15421328 ... -0.03214615 -0.02040114\n  0.04279447], shape=(85742,), dtype=float32)\nshape (2048,)\n2 ,right: 1835","metadata":{}},{"cell_type":"code","source":"#first run train 3 times 12x12->80X80 val\n\nplt.figure(figsize=(32, 32))\nplt.axis('off')\n\n    \nfor idx, (image, label) in enumerate(val_ds.take(9)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n   \n    cos_theta = CosFaceModel_modi(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#first run train 3 times 26x26to80X80 train\nfor idx, (image, label) in enumerate(train_ds.take(16)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T07:12:46.794254Z","iopub.execute_input":"2024-03-31T07:12:46.794684Z","iopub.status.idle":"2024-03-31T07:16:20.397837Z","shell.execute_reply.started":"2024-03-31T07:12:46.794649Z","shell.execute_reply":"2024-03-31T07:16:20.396510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#run train 3 times 26x26->80x80\nfor idx, (image, label) in enumerate(val_ds.take(12)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-31T07:30:40.692511Z","iopub.execute_input":"2024-03-31T07:30:40.693010Z","iopub.status.idle":"2024-03-31T07:33:22.787287Z","shell.execute_reply.started":"2024-03-31T07:30:40.692973Z","shell.execute_reply":"2024-03-31T07:33:22.786178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#first run train 3 times  160x160\nfor idx, (image, label) in enumerate(val_ds.take(6)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T04:55:07.037509Z","iopub.execute_input":"2024-03-23T04:55:07.037991Z","iopub.status.idle":"2024-03-23T04:59:01.624613Z","shell.execute_reply.started":"2024-03-23T04:55:07.037955Z","shell.execute_reply":"2024-03-23T04:59:01.623499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#second run train 3 times  160x160\nfor idx, (image, label) in enumerate(val_ds.take(6)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T02:56:02.985523Z","iopub.execute_input":"2024-03-24T02:56:02.986248Z","iopub.status.idle":"2024-03-24T02:59:58.470094Z","shell.execute_reply.started":"2024-03-24T02:56:02.986207Z","shell.execute_reply":"2024-03-24T02:59:58.469041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#third run train 3 times  80x80\nfor idx, (image, label) in enumerate(val_ds.take(6)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T03:47:20.786262Z","iopub.execute_input":"2024-03-24T03:47:20.786765Z","iopub.status.idle":"2024-03-24T03:48:41.696945Z","shell.execute_reply.started":"2024-03-24T03:47:20.786718Z","shell.execute_reply":"2024-03-24T03:48:41.695801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#third run train 3 times  80x80\nfor idx, (image, label) in enumerate(val_ds.take(6)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T07:20:01.755151Z","iopub.execute_input":"2024-03-24T07:20:01.755508Z","iopub.status.idle":"2024-03-24T07:21:23.405277Z","shell.execute_reply.started":"2024-03-24T07:20:01.755477Z","shell.execute_reply":"2024-03-24T07:21:23.404288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times  112x112\nfor idx, (image, label) in enumerate(val_ds.take(6)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T03:28:13.081494Z","iopub.execute_input":"2024-03-24T03:28:13.081857Z","iopub.status.idle":"2024-03-24T03:30:27.827713Z","shell.execute_reply.started":"2024-03-24T03:28:13.081826Z","shell.execute_reply":"2024-03-24T03:30:27.825825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times  160x160\nfor idx, (image, label) in enumerate(val_ds.take(12)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:09:59.225219Z","iopub.execute_input":"2024-03-18T11:09:59.225556Z","iopub.status.idle":"2024-03-18T11:14:04.59914Z","shell.execute_reply.started":"2024-03-18T11:09:59.225527Z","shell.execute_reply":"2024-03-18T11:14:04.597937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times   80x80\nfor idx, (image, label) in enumerate(val_ds.take(6)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-24T07:03:42.461859Z","iopub.execute_input":"2024-03-24T07:03:42.462215Z","iopub.status.idle":"2024-03-24T07:05:00.052641Z","shell.execute_reply.started":"2024-03-24T07:03:42.462187Z","shell.execute_reply":"2024-03-24T07:05:00.051432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times  112x112\nfor idx, (image, label) in enumerate(val_ds.take(12)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-25T03:11:35.37411Z","iopub.execute_input":"2024-03-25T03:11:35.374508Z","iopub.status.idle":"2024-03-25T03:15:52.758128Z","shell.execute_reply.started":"2024-03-25T03:11:35.374475Z","shell.execute_reply":"2024-03-25T03:15:52.756921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nTo train a model with this dataset you will want the data:\n  To be well shuffled.\n  To be batched.\n  Batches to be available as soon as possible.\n\"\"\"\nbatch_size_per_replica = 256 #128\nglobal_batch_size = batch_size_per_replica * strategy.num_replicas_in_sync\n# global_batch_size = 256\nsteps_per_epoch = int( np.floor(total_examples/float(global_batch_size)) ) # floor as we will drop remind examples\ntrain_ds = train_ds.repeat().batch(global_batch_size, drop_remainder = True).prefetch(buffer_size = tf.data.AUTOTUNE)\n# the training dataset must repeated equal to the number of epochs == train_ds.repeat(num_epochs)\nval_ds = val_ds.repeat().batch(global_batch_size, drop_remainder = True).prefetch(buffer_size = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:16.933666Z","iopub.execute_input":"2024-04-03T03:03:16.934055Z","iopub.status.idle":"2024-04-03T03:03:16.946113Z","shell.execute_reply.started":"2024-04-03T03:03:16.934010Z","shell.execute_reply":"2024-04-03T03:03:16.945318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times  112x112->80x80\nfor idx, (image, label) in enumerate(val_ds.take(12)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-25T03:32:34.032235Z","iopub.execute_input":"2024-03-25T03:32:34.033003Z","iopub.status.idle":"2024-03-25T03:35:16.747873Z","shell.execute_reply.started":"2024-03-25T03:32:34.032964Z","shell.execute_reply":"2024-03-25T03:35:16.746693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times  80x80->112x112\nfor idx, (image, label) in enumerate(val_ds.take(12)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-25T04:19:27.646327Z","iopub.execute_input":"2024-03-25T04:19:27.646659Z","iopub.status.idle":"2024-03-25T04:23:52.242974Z","shell.execute_reply.started":"2024-03-25T04:19:27.646631Z","shell.execute_reply":"2024-03-25T04:23:52.241591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times  112x112->80x80\nfor idx, (image, label) in enumerate(val_ds.take(12)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-25T04:44:38.574476Z","iopub.execute_input":"2024-03-25T04:44:38.574848Z","iopub.status.idle":"2024-03-25T04:47:21.739496Z","shell.execute_reply.started":"2024-03-25T04:44:38.574816Z","shell.execute_reply":"2024-03-25T04:47:21.738214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run train 3 times  80x80->112x112\nfor idx, (image, label) in enumerate(val_ds.take(12)):\n\n   \n    # Convert image from [-1, 1] range to [0, 1] for plt\n    # image = (image + 1.0) / 2.0\n    \n    # Convert image from [0, 255] range to [0, 1] for plt\n    #image = image / 255.0\n    #emb = basic_model(image, training=False)\n\n    #embs = normalize(emb)\n    cos_theta = CosFaceModel(image, training=False)  \n    \n    \n    from keras.utils import to_categorical\n    y = np.array(label)\n    one_hot = to_categorical(y,num_classes=85742)\n    theta = cos_theta\n    #theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon()))\n    target_logits = (theta * 1.0) -0\n    logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * 64 \n    #print(logits)\n    resu = np.argmax(cos_theta,axis= 1)\n    print(cos_theta,resu,label)\n    print('cos_theta',cos_theta[0])\n    y = np.array(label)\n    print('shape',resu.shape)\n    print(str(idx)+' ,right:',np.sum(resu==y))\n    \n    # Plot the image\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-25T05:16:37.146417Z","iopub.execute_input":"2024-03-25T05:16:37.14674Z","iopub.status.idle":"2024-03-25T05:21:02.945228Z","shell.execute_reply.started":"2024-03-25T05:16:37.146711Z","shell.execute_reply":"2024-03-25T05:21:02.943947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from __future__ import print_function\nimport keras\n#from keras.datasets import mnist\nfrom tensorflow.keras.models import Sequential,Model\nfrom tensorflow.keras.layers import Input, Dense, Dropout,Reshape, Flatten\nfrom tensorflow.keras.layers import MaxPooling2D,AveragePooling2D\nfrom tensorflow.keras.layers import Conv2D, BatchNormalization,UpSampling2D\nfrom tensorflow.keras.layers import Add,GlobalAveragePooling2D, Lambda, Conv2D,  Dropout, Dense, Flatten, Activation\nfrom tensorflow.keras.layers import concatenate\nfrom tensorflow.keras.preprocessing.image import load_img,img_to_array\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom tensorflow.keras.layers import LeakyReLU\nfrom tensorflow.keras.layers import concatenate\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras import regularizers\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.callbacks import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import load_model\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:24.123833Z","iopub.execute_input":"2024-04-03T03:03:24.124579Z","iopub.status.idle":"2024-04-03T03:03:24.844740Z","shell.execute_reply.started":"2024-04-03T03:03:24.124532Z","shell.execute_reply":"2024-04-03T03:03:24.843885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt \nimport tensorflow as tf\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:27.314039Z","iopub.execute_input":"2024-04-03T03:03:27.314436Z","iopub.status.idle":"2024-04-03T03:03:27.318553Z","shell.execute_reply.started":"2024-04-03T03:03:27.314333Z","shell.execute_reply":"2024-04-03T03:03:27.317792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import standard dependencies\nimport os\nimport math\nimport cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport PIL\nfrom sklearn.preprocessing import normalize # for normalizing np array to avoid NaN from dividing by 0\nimport glob\n# Import tensorflow dependencies - Functional API\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input, Dropout, BatchNormalization\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:29.097856Z","iopub.execute_input":"2024-04-03T03:03:29.098657Z","iopub.status.idle":"2024-04-03T03:03:36.943812Z","shell.execute_reply.started":"2024-04-03T03:03:29.098625Z","shell.execute_reply":"2024-04-03T03:03:36.942786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_inputconv2 = Input(shape=(40,40,3))","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:36.945131Z","iopub.execute_input":"2024-04-03T03:03:36.945385Z","iopub.status.idle":"2024-04-03T03:03:37.277244Z","shell.execute_reply.started":"2024-04-03T03:03:36.945359Z","shell.execute_reply":"2024-04-03T03:03:37.276404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sliceuse(x,index):\n    return x[:,:,:,index]\ndef splitdata(x,inum,index):\n    da = K.int_shape(x)[1]\n    #print('data num:',da)\n    splitd = (int)(da/inum*2)\n    #test = x[:,0:splitd,0:splitd,:]\n    #print('test',test)\n    \n    if index ==0:\n        return x[:,0:splitd,0:splitd,:]\n    if index ==1:\n        return x[:,0:splitd,splitd:,:]\n    if index ==2:\n        return x[:,splitd:,0:splitd,:]\n    return x[:,splitd:,splitd:,:]\n    \nchanDim = -1","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:40.828917Z","iopub.execute_input":"2024-04-03T03:03:40.829256Z","iopub.status.idle":"2024-04-03T03:03:40.835215Z","shell.execute_reply.started":"2024-04-03T03:03:40.829228Z","shell.execute_reply":"2024-04-03T03:03:40.834368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vgggen5_12_Next40_80_2_model1(x6,outnum):\n    weight_decay = 0.0005    \n    x=Conv2D(filters=32,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x6)\n    x= BatchNormalization(axis=chanDim)(x)\n    x= LeakyReLU(alpha=0.05)(x)       \n  \n    \n    x=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x)\n    x= BatchNormalization(axis=chanDim)(x)\n    x1= LeakyReLU(alpha=0.05)(x) \n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=2,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x1)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5_2= LeakyReLU(alpha=0.05)(x5)    \n    \n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=2,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5_2)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=256,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5_3= LeakyReLU(alpha=0.05)(x5)       \n    \n    x5=Conv2D(filters=32,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5_3)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Flatten()(x5)  \n    \n    x5 = Dense(1200 ,kernel_regularizer=regularizers.l2(weight_decay))(x5)   \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Dense(3200 ,kernel_regularizer=regularizers.l2(weight_decay))(x5)   \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Reshape((10,10,32))(x5)\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)       \n    \n    x5=  Add()([x5,x5_3])\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5= UpSampling2D()(x5)    \n    \n    x5=  Add()([x5,x5_2])\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5= UpSampling2D()(x5)\n    \n    x5=  Add()([x5,x1])\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n\n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)     \n       \n    x7= UpSampling2D()(x5)  #80*80\n    \n    x7=Conv2D(filters=32,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x7)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    \n    x7=Conv2D(filters=32,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x7)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    \n    x7=Conv2D(filters=outnum,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x7)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    return x7\ndef vgggen5_12_Next40_80_2_model2(x6,outnum):\n    weight_decay = 0.0005    \n    x11 = x6\n    x11=Conv2D(filters=64,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x11)\n    x11= BatchNormalization(axis=chanDim)(x11)\n    x11= LeakyReLU(alpha=0.05)(x11)   \n    \n    x11=Conv2D(filters=64,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x11)\n    x11= BatchNormalization(axis=chanDim)(x11)\n    x11= LeakyReLU(alpha=0.05)(x11)   \n    \n    x11= UpSampling2D()(x11)   \n    \n    x11=Conv2D(filters=64,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x11)\n    x11= BatchNormalization(axis=chanDim)(x11)\n    x11= LeakyReLU(alpha=0.05)(x11)      \n    \n    x11_1=Conv2D(filters=64,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x11)\n    x11_1= BatchNormalization(axis=chanDim)(x11_1)\n    x11_1= LeakyReLU(alpha=0.05)(x11_1)      \n    \n    x11=  Add()([x11,x11_1])\n    \n    x11=Conv2D(filters=outnum,kernel_size=(5,5),strides=1,padding=\"same\",activation=\"relu\",kernel_regularizer=regularizers.l2(weight_decay))(x11)\n    return x11\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:43.319171Z","iopub.execute_input":"2024-04-03T03:03:43.319856Z","iopub.status.idle":"2024-04-03T03:03:56.233349Z","shell.execute_reply.started":"2024-04-03T03:03:43.319816Z","shell.execute_reply":"2024-04-03T03:03:56.232272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vgggen5_12_Next40_80_6(inputs):\n    weight_decay = 0.0005    \n       \n   \n    x6 = inputs\n    x6 = Reshape((40,40,3))(x6)\n    \n    x7 = vgggen5_12_Next40_80_2_model1(x6,3)\n    \n    \n    \n    x11 = vgggen5_12_Next40_80_2_model2(x6,3)\n    \n    \n    \n    \n    x=  Add()([x7,x11])  #x3resu,\n    #xrsu = Merge([ x,x2,x3],mode='concat')\n    model = Model(inputs=[inputs],outputs=x,name='vgggen5_12_Next40_80_6')\n    return model\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vgggen5_12_Next40_80_2_model3(x6,outnum):\n    weight_decay = 0.0005    \n    x=Conv2D(filters=32,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x6)\n    x= BatchNormalization(axis=chanDim)(x)\n    x= LeakyReLU(alpha=0.05)(x)       \n  \n    \n    x=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x)\n    x= BatchNormalization(axis=chanDim)(x)\n    x1= LeakyReLU(alpha=0.05)(x) #80x80\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=2,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x1)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)  #40x40\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5_2= LeakyReLU(alpha=0.05)(x5)    \n    \n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=2,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5_2)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)  #20x20\n    \n    x5=Conv2D(filters=256,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5_3= LeakyReLU(alpha=0.05)(x5)  #20x20\n    \n    x5=Conv2D(filters=32,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5_3)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Flatten()(x5)  \n    \n    x5 = Dense(1600 ,kernel_regularizer=regularizers.l2(weight_decay))(x5)         \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Dense(1200 ,kernel_regularizer=regularizers.l2(weight_decay))(x5)         \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Dense(2000 ,kernel_regularizer=regularizers.l2(weight_decay))(x5)         \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Reshape((20,20,5))(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)       \n    \n    x5=  Add()([x5,x5_3])\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5= UpSampling2D()(x5)    \n    \n    x5=  Add()([x5,x5_2])   #40x40\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5= UpSampling2D()(x5)\n    \n    x5=  Add()([x5,x1])  #80x80\n    \n       \n    x7=Conv2D(filters=128,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    \n    x7=Conv2D(filters=64,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x7)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    \n    x7=Conv2D(filters=outnum,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x7)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    return x7\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:56.235038Z","iopub.execute_input":"2024-04-03T03:03:56.235323Z","iopub.status.idle":"2024-04-03T03:03:56.432655Z","shell.execute_reply.started":"2024-04-03T03:03:56.235294Z","shell.execute_reply":"2024-04-03T03:03:56.431844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vgggen5_12_Next40_80_2_model5(x6,outnum):\n    weight_decay = 0.0005    \n    x=Conv2D(filters=32,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x6)\n    x= BatchNormalization(axis=chanDim)(x)\n    x= LeakyReLU(alpha=0.05)(x)       \n  \n    \n    x=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x)\n    x= BatchNormalization(axis=chanDim)(x)\n    x1= LeakyReLU(alpha=0.05)(x) #80x80\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=2,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x1)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)  #40x40\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5_2= LeakyReLU(alpha=0.05)(x5)    \n    \n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=2,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5_2)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)  #20x20\n    \n    x5=Conv2D(filters=256,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5_3= LeakyReLU(alpha=0.05)(x5)  #20x20\n    \n    x5=Conv2D(filters=32,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5_3)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Flatten()(x5)  \n    \n    x5 = Dense(1600 ,kernel_regularizer=regularizers.l2(weight_decay))(x5)  \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Dense(1200 ,kernel_regularizer=regularizers.l2(weight_decay))(x5)  \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Dense(2000 ,kernel_regularizer=regularizers.l2(weight_decay))(x5) \n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5 = Reshape((20,20,5))(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)       \n    \n    x5=  Add()([x5,x5_3])\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5= UpSampling2D()(x5)    \n    \n    x5=  Add()([x5,x5_2])   #40x40\n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)\n    \n    x5=Conv2D(filters=128,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5=Conv2D(filters=64,kernel_size=(3,3),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x5= BatchNormalization(axis=chanDim)(x5)\n    x5= LeakyReLU(alpha=0.05)(x5)    \n    \n    x5= UpSampling2D()(x5)\n    \n    x5=  Add()([x5,x1])  #80x80\n    \n       \n    x7=Conv2D(filters=128,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x5)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    \n    x7=Conv2D(filters=64,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x7)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    \n    x7=Conv2D(filters=outnum,kernel_size=(5,5),strides=1,padding=\"same\",kernel_regularizer=regularizers.l2(weight_decay))(x7)\n    x7= BatchNormalization(axis=chanDim)(x7)\n    x7= LeakyReLU(alpha=0.05)(x7)\n    return x7\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    vgggen5_16_Next =vgggen5_12_Next40_80_6(model_inputconv2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def modevgg16_gen_20_6(inputs):      \n    with strategy.scope():\n        vgggen5_16_Next.trainable = True    \n       \n        x =  inputs\n        #x = AveragePooling2D((2, 2))(x)\n        #x = AveragePooling2D((2, 2))(x)\n        xrsu = vgggen5_16_Next(x)\n        \n        #xrsu = concatenate([x2,x3,x5])\n        layer_model = Model(inputs=inputs,outputs=xrsu)\n    return layer_model\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_inputconv3 = Input(shape=(80,80,3))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vgggen5_12_Next40_80_2_model5_2(inputs):      \n    with strategy.scope():\n        #vgggen5_16_Next.trainable = True    \n       \n        x =  inputs\n        #x = AveragePooling2D((2, 2))(x)\n        #x = AveragePooling2D((2, 2))(x)\n        xrsu = vgggen5_12_Next40_80_2_model5(x,3)\n        \n        #xrsu = concatenate([x2,x3,x5])\n        layer_model = Model(inputs=inputs,outputs=xrsu)\n    return layer_model\nwith strategy.scope():\n    vgggen5_12_Next40_80_2_model5_2 =vgggen5_12_Next40_80_2_model5_2(model_inputconv3)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def modevgg16_gen_20_8(inputs):      \n    with strategy.scope():\n        modevgg16_gen_20_6.trainable = True    \n       \n        x =  inputs\n        #x = AveragePooling2D((2, 2))(x)\n        #x = AveragePooling2D((2, 2))(x)\n        xrsu = modevgg16_gen_20_6(x,3)\n        xresu2 = vgggen5_12_Next40_80_2_model5_2(xrsu)\n        \n        #xrsu = concatenate([x2,x3,x5])\n        layer_model = Model(inputs=inputs,outputs=xresu2)\n    return layer_model","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:56.433541Z","iopub.execute_input":"2024-04-03T03:03:56.433801Z","iopub.status.idle":"2024-04-03T03:03:56.641427Z","shell.execute_reply.started":"2024-04-03T03:03:56.433773Z","shell.execute_reply":"2024-04-03T03:03:56.640674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    vgggen5_16_Next =vgggen5_12_Next40_80_6(model_inputconv2)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:03:56.642777Z","iopub.execute_input":"2024-04-03T03:03:56.643077Z","iopub.status.idle":"2024-04-03T03:04:02.161423Z","shell.execute_reply.started":"2024-04-03T03:03:56.643050Z","shell.execute_reply":"2024-04-03T03:04:02.160272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modevgg16_gen_20_8 = modevgg16_gen_20_8(model_inputconv2)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:04:29.446760Z","iopub.execute_input":"2024-04-03T03:04:29.447156Z","iopub.status.idle":"2024-04-03T03:04:34.561648Z","shell.execute_reply.started":"2024-04-03T03:04:29.447125Z","shell.execute_reply":"2024-04-03T03:04:34.560714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=.0051, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)  #.0051\n\nmodevgg16_gen_20_6.compile(optimizer = opt, loss = msesum,  metrics = ['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:04:40.727661Z","iopub.execute_input":"2024-04-03T03:04:40.727943Z","iopub.status.idle":"2024-04-03T03:04:40.976176Z","shell.execute_reply.started":"2024-04-03T03:04:40.727907Z","shell.execute_reply":"2024-04-03T03:04:40.975223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modevgg16_gen_20_6.load_weights('/kaggle/input/tpu40to80data3/face_CNN_model_finalvgg'+str(56))","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:04:41.060088Z","iopub.execute_input":"2024-04-03T03:04:41.060323Z","iopub.status.idle":"2024-04-03T03:04:41.850572Z","shell.execute_reply.started":"2024-04-03T03:04:41.060296Z","shell.execute_reply":"2024-04-03T03:04:41.849791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = 80\ndef execprev():\n    #base_model = tf.keras.applications.EfficientNetV2B0(include_top=False, weights=\"imagenet\", input_shape=(80, 80, 3))\n    base_model = tf.keras.applications.xception.Xception(include_top=False, weights=\"imagenet\", input_shape=(size, size, 3))\n#     base_model.trainable = False # Freeze the model\n    \n#     # Unfreeze the top 20 layers while leaving BatchNorm layers frozen\n#     for layer in base_model.layers[-20:]:\n#         if not isinstance(layer, BatchNormalization):\n#             layer.trainable = True\n    \n    avg_pool = GlobalAveragePooling2D(name=\"avg_pool\")(base_model.output)  # Pass the output tensor of base_model to GlobalAveragePooling2D\n    \n    top_dropout_rate = 0.2\n    top_dropout = Dropout(top_dropout_rate, name=\"top_dropout\")(avg_pool)\n    \n    \n    return top_dropout,base_model\nwith strategy.scope():\n    top_dropout,base_model = execprev()\ndef build_embedding():\n    #top_dropout,base_model = execprev()\n    dense_512 = Dense(units=512, activation=None, name=\"pred\")(top_dropout)  # No activation on the final dense layer == linear activation\n    embedding = BatchNormalization(momentum=.99, epsilon=.001, scale=True, name=\"embedding\")(dense_512)\n    # build the embedding model and return it\n    return Model(base_model.input, embedding, name=\"embedding\")\n\ndef build_embedding2():\n    #top_dropout,base_model = execprev()\n    dense_256 = Dense(units=256, activation=None, name=\"pred\")(top_dropout)  # No activation on the final dense layer == linear activation\n    embedding = BatchNormalization(momentum=.99, epsilon=.001, scale=True, name=\"embedding\")(dense_256)\n    # build the embedding model and return it\n    return Model(base_model.input, embedding, name=\"embedding_256\")\ndef build_embedding3():\n    #top_dropout,base_model = execprev()\n    dense_128 = Dense(units=128, activation=None, name=\"pred\")(top_dropout)  # No activation on the final dense layer == linear activation\n    embedding = BatchNormalization(momentum=.99, epsilon=.001, scale=True, name=\"embedding\")(dense_128)\n    # build the embedding model and return it\n    return Model(base_model.input, embedding, name=\"embedding_128\")","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:04:47.216982Z","iopub.execute_input":"2024-04-03T03:04:47.217351Z","iopub.status.idle":"2024-04-03T03:05:00.795032Z","shell.execute_reply.started":"2024-04-03T03:04:47.217313Z","shell.execute_reply":"2024-04-03T03:05:00.793842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import backend, layers, metrics\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import Xception\nfrom tensorflow.keras.models import Model, Sequential\n\nfrom tensorflow.keras.utils import plot_model\nfrom sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n        \ndef build_embedding2():\n    base_model = Xception(\n        input_shape = (80, 80, 3),\n        weights='imagenet',\n        include_top=False,\n        pooling='avg',\n    )\n     \n#     base_model.trainable = False # Freeze the model\n    \n#     # Unfreeze the top 20 layers while leaving BatchNorm layers frozen\n#     for layer in base_model.layers[-20:]:\n#         if not isinstance(layer, BatchNormalization):\n#             layer.trainable = True\n    \n    #avg_pool = GlobalAveragePooling2D(name=\"avg_pool\")(base_model.output)  # Pass the output tensor of base_model to GlobalAveragePooling2D\n    \n    top_dropout_rate = 0.2\n    top_dropout = Dropout(top_dropout_rate, name=\"top_dropout\")(base_model.output)\n    \n    dense_512 = Dense(units=512, activation=None, name=\"pred\")(top_dropout)  # No activation on the final dense layer == linear activation\n    embedding = BatchNormalization(momentum=.99, epsilon=.001, scale=True, name=\"embedding\")(dense_512)\n    # build the embedding model and return it\n    return Model(base_model.input, embedding, name=\"embedding\")","metadata":{"execution":{"iopub.status.busy":"2024-03-15T02:08:40.087397Z","iopub.execute_input":"2024-03-15T02:08:40.087814Z","iopub.status.idle":"2024-03-15T02:08:40.167742Z","shell.execute_reply.started":"2024-03-15T02:08:40.087781Z","shell.execute_reply":"2024-03-15T02:08:40.166956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.keras.utils.register_keras_serializable()\nclass NormDense(tf.keras.layers.Layer):\n    def __init__(self, units=1000, **kwargs):\n        self.units = units\n        super(NormDense, self).__init__(**kwargs)\n        #self.build(input_shape)\n        #super(NormDense,self)._init_(**kwargs)\n        \n        # self.kernel_regularizer\n    def build(self, input_shape):\n            self.w = self.add_weight(\n                name=\"norm_dense_w\",\n                shape=(input_shape[-1], self.units),\n                initializer=tf.keras.initializers.glorot_normal(), # glorot_uniform()\n                trainable=True\n            )\n\n   \n    def call(self, inputs, **kwargs):\n        \n        norm_w = tf.nn.l2_normalize(self.w, axis=0, epsilon=1e-5) # each column is a weight vector\n        norm_embedding = tf.nn.l2_normalize(inputs, axis=1, epsilon=1e-5)\n        cos_theta = tf.linalg.matmul(norm_embedding, norm_w, name='cos_theta')\n        return cos_theta\n\n    def compute_output_shape(self, input_shape):\n        return (input_shape[0], self.units)\n  \n\n\n    def get_config(self):\n        config = super().get_config()\n        config[\"w\"] = self.w\n        return config ","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:05:00.796295Z","iopub.execute_input":"2024-04-03T03:05:00.796546Z","iopub.status.idle":"2024-04-03T03:05:00.803926Z","shell.execute_reply.started":"2024-04-03T03:05:00.796520Z","shell.execute_reply":"2024-04-03T03:05:00.803178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CosfaceLoss(tf.keras.losses.Loss):\n    def __init__(self, num_classes=85742, GLOBAL_BATCH_SIZE = 1024, margin1=1.0, margin2=0.005, margin3=0.0, scale=64.0, **kwargs):\n        super(CosfaceLoss, self).__init__(**kwargs)\n        self.num_classes = num_classes\n        self.margin1, self.margin2, self.margin3, self.scale = margin1, margin2, margin3, scale\n        #self.loss = tf.keras.losses.categorical_crossentropy()\n        # self.threshold = np.cos((np.pi - margin2) / margin1)  # grad(theta) == 0\n        # self.theta_margin_min = (-1 - margin3) * 2\n        #self.loss_obj = tf.keras.losses.CategoricalCrossentropy(reduction=tf.keras.losses.Reduction.NONE, from_logits = True) # Linear activation input\n        self.loss_obj = tf.keras.losses.CategoricalCrossentropy(reduction=tf.keras.losses.Reduction.NONE, from_logits = True) # Linear activation input\n        self.GLOBAL_BATCH_SIZE = GLOBAL_BATCH_SIZE\n    def call(self, y_true, cos_theta):\n        one_hot = tf.one_hot(tf.squeeze(y_true, axis=-1), depth = self.num_classes)\n        theta = (tf.keras.backend.clip(cos_theta, -1.0 + tf.keras.backend.epsilon(), 1.0 - tf.keras.backend.epsilon())) #tf.math.acos tf.math.acos\n        target_logits = (theta * self.margin1 + self.margin2) - self.margin3 #tf.cos tf.cos\n        logits = (cos_theta * (1.0 - one_hot) + target_logits * one_hot) * self.scale\n        return tf.nn.compute_average_loss(self.loss_obj(one_hot, logits), global_batch_size = self.GLOBAL_BATCH_SIZE)\n\n    def get_config(self):\n        config = super(ArcfaceLoss, self).get_config()\n        config.update(\n            {\n                \"margin1\": self.margin1,\n                \"margin2\": self.margin2,\n                \"margin3\": self.margin3,\n                \"scale\": self.scale\n            }\n        )\n        return config","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:05:00.804854Z","iopub.execute_input":"2024-04-03T03:05:00.805111Z","iopub.status.idle":"2024-04-03T03:05:00.819811Z","shell.execute_reply.started":"2024-04-03T03:05:00.805086Z","shell.execute_reply":"2024-04-03T03:05:00.819047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=.0051, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)  #.0051\ncos_loss = CosfaceLoss(num_classes = 85742)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:05:00.821305Z","iopub.execute_input":"2024-04-03T03:05:00.821545Z","iopub.status.idle":"2024-04-03T03:05:00.835354Z","shell.execute_reply.started":"2024-04-03T03:05:00.821521Z","shell.execute_reply":"2024-04-03T03:05:00.834689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    basic_model = build_embedding()\n    inputs = basic_model.inputs[0] # Model may have multiple inputs\n    embeddings = basic_model.outputs[0] # Model may have multiple outputs\n    classification_output_layer = NormDense(units=num_classes)(embeddings)\n    \n    CosFaceModel = Model(inputs, classification_output_layer)\n    \n    CosFaceModel.compile(optimizer = opt, loss = cos_loss) #, metrics = [\"accuracy\"]","metadata":{"execution":{"iopub.status.busy":"2024-03-19T02:22:54.167492Z","iopub.execute_input":"2024-03-19T02:22:54.168345Z","iopub.status.idle":"2024-03-19T02:22:56.779233Z","shell.execute_reply.started":"2024-03-19T02:22:54.168307Z","shell.execute_reply":"2024-03-19T02:22:56.778178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    basic_model = build_embedding2()\n    inputs = basic_model.inputs[0] # Model may have multiple inputs\n    embeddings = basic_model.outputs[0] # Model may have multiple outputs\n    classification_output_layer = NormDense(units=num_classes)(embeddings)\n    \n    CosFaceModel = Model(inputs, classification_output_layer)\n    \n    CosFaceModel.compile(optimizer = opt, loss = cos_loss) #, metrics = [\"accuracy\"]","metadata":{"execution":{"iopub.status.busy":"2024-03-24T05:06:55.700716Z","iopub.execute_input":"2024-03-24T05:06:55.701044Z","iopub.status.idle":"2024-03-24T05:06:58.410704Z","shell.execute_reply.started":"2024-03-24T05:06:55.701016Z","shell.execute_reply":"2024-03-24T05:06:58.409885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    basic_model = build_embedding3()\n    inputs = basic_model.inputs[0] # Model may have multiple inputs\n    embeddings = basic_model.outputs[0] # Model may have multiple outputs\n    classification_output_layer = NormDense(units=num_classes)(embeddings)\n    \n    CosFaceModel = Model(inputs, classification_output_layer)\n    \n    CosFaceModel.compile(optimizer = opt, loss = cos_loss) #, metrics = [\"accuracy\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:21:39.565329Z","iopub.execute_input":"2024-04-03T01:21:39.565661Z","iopub.status.idle":"2024-04-03T01:21:41.177992Z","shell.execute_reply.started":"2024-04-03T01:21:39.565632Z","shell.execute_reply":"2024-04-03T01:21:41.176920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ExitOnNaN(tf.keras.callbacks.Callback):\n    \"\"\"Callback that exit directly when a NaN loss is encountered, avoiding saving model\"\"\"\n\n    def __init__(self):\n        super().__init__()\n        self._supports_tf_logs = True\n\n    def on_batch_end(self, batch, logs=None):\n        logs = logs or {}\n        loss = logs.get(\"loss\")\n        if loss is not None:\n            if not tf.math.is_finite(loss):\n                print(\"\\nError: Invalid loss, terminating training\")\n                self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:05:01.989567Z","iopub.execute_input":"2024-04-03T03:05:01.989933Z","iopub.status.idle":"2024-04-03T03:05:01.995190Z","shell.execute_reply.started":"2024-04-03T03:05:01.989901Z","shell.execute_reply":"2024-04-03T03:05:01.994448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_path = '/kaggle/working/chekpoints'\n\n\n\ncallbacks = [ExitOnNaN()]","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:05:04.263667Z","iopub.execute_input":"2024-04-03T03:05:04.264423Z","iopub.status.idle":"2024-04-03T03:05:04.267800Z","shell.execute_reply.started":"2024-04-03T03:05:04.264391Z","shell.execute_reply":"2024-04-03T03:05:04.267042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#First run\nCosFaceModel2.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-18T00:50:13.225611Z","iopub.execute_input":"2024-03-18T00:50:13.226885Z","iopub.status.idle":"2024-03-18T01:28:28.021654Z","shell.execute_reply.started":"2024-03-18T00:50:13.226837Z","shell.execute_reply":"2024-03-18T01:28:28.020659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#second run\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-18T01:31:59.564742Z","iopub.execute_input":"2024-03-18T01:31:59.565151Z","iopub.status.idle":"2024-03-18T02:09:45.244897Z","shell.execute_reply.started":"2024-03-18T01:31:59.565114Z","shell.execute_reply":"2024-03-18T02:09:45.243837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#third run\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:13:24.755615Z","iopub.execute_input":"2024-03-18T02:13:24.756118Z","iopub.status.idle":"2024-03-18T02:50:45.235614Z","shell.execute_reply.started":"2024-03-18T02:13:24.756047Z","shell.execute_reply":"2024-03-18T02:50:45.234585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#First 80x80 run\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-18T03:03:17.394106Z","iopub.execute_input":"2024-03-18T03:03:17.394458Z","iopub.status.idle":"2024-03-18T03:31:09.86799Z","shell.execute_reply.started":"2024-03-18T03:03:17.394428Z","shell.execute_reply":"2024-03-18T03:31:09.866968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Second 80x80 run\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-18T03:33:56.402394Z","iopub.execute_input":"2024-03-18T03:33:56.402726Z","iopub.status.idle":"2024-03-18T04:01:02.825721Z","shell.execute_reply.started":"2024-03-18T03:33:56.402697Z","shell.execute_reply":"2024-03-18T04:01:02.824524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Third 80x80 run\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-18T04:04:01.766487Z","iopub.execute_input":"2024-03-18T04:04:01.766901Z","iopub.status.idle":"2024-03-18T04:30:56.573992Z","shell.execute_reply.started":"2024-03-18T04:04:01.766865Z","shell.execute_reply":"2024-03-18T04:30:56.573015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=.051, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)\ncos_loss = CosfaceLoss(num_classes = 85742)\nwith strategy.scope():\n    basic_model = build_embedding3()\n    inputs = basic_model.inputs[0] # Model may have multiple inputs\n    embeddings = basic_model.outputs[0] # Model may have multiple outputs\n    classification_output_layer = NormDense(units=num_classes)(embeddings)\n    \n    CosFaceModel = Model(inputs, classification_output_layer)\n    \n    CosFaceModel.compile(optimizer = opt, loss = cos_loss)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:09:36.453336Z","iopub.execute_input":"2024-04-03T03:09:36.453969Z","iopub.status.idle":"2024-04-03T03:09:38.150692Z","shell.execute_reply.started":"2024-04-03T03:09:36.453928Z","shell.execute_reply":"2024-04-03T03:09:38.149530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def CosFaceModel_combin(inputs):      \n    with strategy.scope():\n        modevgg16_gen_20_6.trainable = False    \n       \n        x =  inputs\n        #x = AveragePooling2D((2, 2))(x)\n        #x = AveragePooling2D((2, 2))(x)\n        xtarget = modevgg16_gen_20_6(x)\n        xrsu = CosFaceModel(xtarget)\n        #xrsu = concatenate([x2,x3,x5])\n        layer_model = Model(inputs=inputs,outputs=[xtarget,xrsu])\n    return layer_model\n\nmy_loss = {   \n    'xtarget': msesum,\\\n    'xrsu':cos_loss\n  }\nmy_loss_weights = {\n    'xtarget':1,\\\n    'xrsu':1\n    }\nmy_metrics ={\n    'xtarget':[\"accuracy\"],  \n    'xrsu':[\"accuracy\"]\n    }","metadata":{"execution":{"iopub.status.busy":"2024-04-02T06:57:41.013946Z","iopub.execute_input":"2024-04-02T06:57:41.014316Z","iopub.status.idle":"2024-04-02T06:57:41.019661Z","shell.execute_reply.started":"2024-04-02T06:57:41.014274Z","shell.execute_reply":"2024-04-02T06:57:41.018911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 两个输出层\noutput1 = Dense(10, activation='softmax')(hidden2)\noutput2 = Dense(5, activation='softmax')(hidden2)\n \n# 创建模型\nmodel = Model(inputs=inputs, outputs=[output1, output2])\n \n# 编译模型\nmodel.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])\n \n# 模型的使用方法\n# x = np.random.random((100, 784))  # 示例输入数据\n# y1 = np.random.random((100, 10))  # 示例目标输出数据1\n# y2 = np.random.random((100, 5))   # 示例目标输出数据2\n# model.fit(x, [y1, y2])\n\nmodel.compile(optimizer='adam', \n              loss=['categorical_crossentropy', 'binary_crossentropy'], \n              metrics=['accuracy'])\n\nmodel.fit(X, [y1, y2], epochs=10, batch_size=32)\n\npredictions = model.predict(X_test)\n\noutput1_predictions = predictions[0]\noutput2_predictions = predictions[1]\n\nprint(output1_predictions)\nprint(output2_predictions)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def CosFaceModel_combin3(inputs):      \n    with strategy.scope():\n        modevgg16_gen_20_8.trainable = True\n        CosFaceModel.trainable = True\n        x =  inputs\n        #x = AveragePooling2D((2, 2))(x)\n        #x = AveragePooling2D((2, 2))(x)\n        xrsu = modevgg16_gen_20_8(x)\n        xrsu = CosFaceModel(xrsu)\n        #xrsu = concatenate([x2,x3,x5])\n        layer_model = Model(inputs=inputs,outputs=xrsu)\n    return layer_model\nopt = tf.keras.optimizers.Adam(learning_rate=.0016, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)\nCosFaceModel_modi = CosFaceModel_combin3(model_inputconv2)\nwith strategy.scope():        \n    CosFaceModel_modi.compile(optimizer = opt, loss = cos_loss)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#basic_model.load_weights('/kaggle/input/tputraindata15/basic_model80.weights18.h5')\n#CosFaceModel.load_weights('/kaggle/input/tputraindata15/CosFaceModelmodi80.weights18.h5')  \n#modevgg16_gen_20_8.load_weights('/kaggle/input/tputraindata23/face_CNN_model_finalvgg59')\n#modevgg16_gen_20_8.load_weights('/kaggle/input/tputraindata27/face_CNN_model_finalvgg59')\n#modevgg16_gen_20_8.load_weights('face_CNN_model_finalvgg59')\nCosFaceModel_modi.load_weights('face_CNN_model_CosFaceModel_modi')\n#CosFaceModel_modi.load_weights('/kaggle/input/tputraindata28/face_CNN_model_CosFaceModel_modi')\n\n#CosFaceModel_modi.load_weights('face_CNN_model_CosFaceModel_modi')\n#modevgg16_gen_20_8.save_weights('face_CNN_model_finalvgg'+str(59))\n\nmodevgg16_gen_20_6.trainable = False\nbasic_model.trainable = False\nCosFaceModel.trainable = False\nprint( basic_model.trainable)\nprint( CosFaceModel.trainable)\n\nprint( modevgg16_gen_20_6.trainable)\nprint( modevgg16_gen_20_8.trainable)\n\nprint( vgggen5_12_Next40_80_2_model5_2.trainable)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modevgg16_gen_20_8.save_weights('face_CNN_model_finalvgg'+str(59))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 12x12 to 80x80 model 80x80 \nCosFaceModel_modi.fit(train_ds,\n                      epochs=3,  \n                      callbacks=callbacks, \n                      steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print( modevgg16_gen_20_6.trainable)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T01:17:14.237549Z","iopub.execute_input":"2024-04-02T01:17:14.238465Z","iopub.status.idle":"2024-04-02T01:17:14.243252Z","shell.execute_reply.started":"2024-04-02T01:17:14.238420Z","shell.execute_reply":"2024-04-02T01:17:14.242311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nTo train a model with this dataset you will want the data:\n  To be well shuffled.\n  To be batched.\n  Batches to be available as soon as possible.\n\"\"\"\nbatch_size_per_replica = 256 #128\nglobal_batch_size = batch_size_per_replica * strategy.num_replicas_in_sync\n# global_batch_size = 256\nsteps_per_epoch = int( np.floor(total_examples/float(global_batch_size)) ) # floor as we will drop remind examples\ntrain_ds = train_ds.repeat().batch(global_batch_size, drop_remainder = True).prefetch(buffer_size = tf.data.AUTOTUNE)\n# the training dataset must repeated equal to the number of epochs == train_ds.repeat(num_epochs)\nval_ds = val_ds.repeat().batch(global_batch_size, drop_remainder = True).prefetch(buffer_size = tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:14:47.218594Z","iopub.execute_input":"2024-04-03T03:14:47.219525Z","iopub.status.idle":"2024-04-03T03:14:47.228139Z","shell.execute_reply.started":"2024-04-03T03:14:47.219484Z","shell.execute_reply":"2024-04-03T03:14:47.227183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=.011, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)","metadata":{"execution":{"iopub.status.busy":"2024-04-01T12:01:36.201681Z","iopub.execute_input":"2024-04-01T12:01:36.202136Z","iopub.status.idle":"2024-04-01T12:01:36.209466Z","shell.execute_reply.started":"2024-04-01T12:01:36.202098Z","shell.execute_reply":"2024-04-01T12:01:36.208290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.load_weights('/kaggle/input/tputraindata13/CosFaceModelmodi80.weights17.h5')  ","metadata":{"execution":{"iopub.status.busy":"2024-04-01T13:20:23.426397Z","iopub.execute_input":"2024-04-01T13:20:23.426675Z","iopub.status.idle":"2024-04-01T13:20:29.943205Z","shell.execute_reply.started":"2024-04-01T13:20:23.426647Z","shell.execute_reply":"2024-04-01T13:20:29.941874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.load_weights('/kaggle/input/tputraindata13/basic_model80.weights17.h5')","metadata":{"execution":{"iopub.status.busy":"2024-04-01T13:20:10.130359Z","iopub.execute_input":"2024-04-01T13:20:10.131362Z","iopub.status.idle":"2024-04-01T13:20:15.920280Z","shell.execute_reply.started":"2024-04-01T13:20:10.131314Z","shell.execute_reply":"2024-04-01T13:20:15.919057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modevgg16_gen_20_6.load_weights('/kaggle/input/tpu40to80data3/face_CNN_model_finalvgg'+str(56))","metadata":{"execution":{"iopub.status.busy":"2024-04-01T13:21:25.727438Z","iopub.execute_input":"2024-04-01T13:21:25.728504Z","iopub.status.idle":"2024-04-01T13:21:26.278281Z","shell.execute_reply.started":"2024-04-01T13:21:25.728465Z","shell.execute_reply":"2024-04-01T13:21:26.277300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.save_weights('basic_model80.weights17.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-31T07:29:57.812930Z","iopub.execute_input":"2024-03-31T07:29:57.813308Z","iopub.status.idle":"2024-03-31T07:29:58.887527Z","shell.execute_reply.started":"2024-03-31T07:29:57.813278Z","shell.execute_reply":"2024-03-31T07:29:58.886242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.save_weights('CosFaceModelmodi80.weights17.h5') ","metadata":{"execution":{"iopub.status.busy":"2024-03-31T07:30:01.291622Z","iopub.execute_input":"2024-03-31T07:30:01.292033Z","iopub.status.idle":"2024-03-31T07:30:02.491464Z","shell.execute_reply.started":"2024-03-31T07:30:01.292001Z","shell.execute_reply":"2024-03-31T07:30:02.490252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.load_weights('basic_model80.weights17.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-31T07:17:00.390295Z","iopub.execute_input":"2024-03-31T07:17:00.390906Z","iopub.status.idle":"2024-03-31T07:17:04.383761Z","shell.execute_reply.started":"2024-03-31T07:17:00.390871Z","shell.execute_reply":"2024-03-31T07:17:04.382684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.load_weights('CosFaceModelmodi80.weights17.h5') ","metadata":{"execution":{"iopub.status.busy":"2024-03-31T07:17:04.385504Z","iopub.execute_input":"2024-03-31T07:17:04.385808Z","iopub.status.idle":"2024-03-31T07:17:09.674643Z","shell.execute_reply.started":"2024-03-31T07:17:04.385777Z","shell.execute_reply":"2024-03-31T07:17:09.673490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.save_weights('basic_model80.weights18.h5')\nCosFaceModel.save_weights('CosFaceModelmodi80.weights18.h5')  \nmodevgg16_gen_20_6.save_weights('face_CNN_model_finalvgg'+str(56))","metadata":{"execution":{"iopub.status.busy":"2024-04-02T05:45:37.600707Z","iopub.execute_input":"2024-04-02T05:45:37.601099Z","iopub.status.idle":"2024-04-02T05:45:40.575818Z","shell.execute_reply.started":"2024-04-02T05:45:37.601068Z","shell.execute_reply":"2024-04-02T05:45:40.574711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modevgg16_gen_20_7.save_weights('face_CNN_model_finalvgg'+str(57))","metadata":{"execution":{"iopub.status.busy":"2024-04-03T02:49:59.759159Z","iopub.execute_input":"2024-04-03T02:49:59.759527Z","iopub.status.idle":"2024-04-03T02:50:03.131797Z","shell.execute_reply.started":"2024-04-03T02:49:59.759496Z","shell.execute_reply":"2024-04-03T02:50:03.130617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modevgg16_gen_20_7.load_weights('face_CNN_model_finalvgg'+str(57))","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:11:49.123528Z","iopub.execute_input":"2024-04-03T03:11:49.123902Z","iopub.status.idle":"2024-04-03T03:11:50.543376Z","shell.execute_reply.started":"2024-04-03T03:11:49.123859Z","shell.execute_reply":"2024-04-03T03:11:50.542513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.load_weights('basic_model80.weights18.h5')\nCosFaceModel.load_weights('CosFaceModelmodi80.weights18.h5')  \nmodevgg16_gen_20_6.load_weights('face_CNN_model_finalvgg'+str(56))","metadata":{"execution":{"iopub.status.busy":"2024-04-02T05:30:40.346251Z","iopub.execute_input":"2024-04-02T05:30:40.346680Z","iopub.status.idle":"2024-04-02T05:30:50.192779Z","shell.execute_reply.started":"2024-04-02T05:30:40.346647Z","shell.execute_reply":"2024-04-02T05:30:50.191674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=.0016, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)\ncos_loss = CosfaceLoss(num_classes = 85742)\nwith strategy.scope():\n    basic_model = build_embedding3()\n    inputs = basic_model.inputs[0] # Model may have multiple inputs\n    embeddings = basic_model.outputs[0] # Model may have multiple outputs\n    classification_output_layer = NormDense(units=num_classes)(embeddings)\n    \n    CosFaceModel = Model(inputs, classification_output_layer)\n    \n    CosFaceModel.compile(optimizer = opt, loss = cos_loss)\nbasic_model.load_weights('basic_model80.weights18.h5')\nCosFaceModel.load_weights('CosFaceModelmodi80.weights18.h5')  ","metadata":{"execution":{"iopub.status.busy":"2024-04-02T05:30:27.723044Z","iopub.execute_input":"2024-04-02T05:30:27.723456Z","iopub.status.idle":"2024-04-02T05:30:37.864869Z","shell.execute_reply.started":"2024-04-02T05:30:27.723424Z","shell.execute_reply":"2024-04-02T05:30:37.863827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = Model(inputs=[input_gen, input_id], outputs=[dis_out, output_id,output_f, output_b ,dis_out2])\n    \n#model.compile(loss=['mse', 'mae', 'mae', 'mae','mse'], loss_weights=[1, 5, 10, 10,10], optimizer=Adam(learning_rate=2e-4))\ncos_loss = CosfaceLoss(num_classes = 85742)\ndef msesum(y_true, y_pred):\n    return K.sum(K.abs(y_pred - y_true), axis=-1)*100\ndef CosFaceModel_combin(inputs):      \n    with strategy.scope():\n        modevgg16_gen_20_6.trainable = True   \n       \n        x =  inputs\n        #x = AveragePooling2D((2, 2))(x)\n        #x = AveragePooling2D((2, 2))(x)\n        xtarget = modevgg16_gen_20_6(x)\n        xrsu = CosFaceModel(xtarget)\n        #xrsu = concatenate([x2,x3,x5])\n        #outputs = [xtarget, xrsu]\n\n        layer_model = Model(inputs=inputs,outputs=xrsu)\n    return layer_model\n\nopt = tf.keras.optimizers.Adam(learning_rate=.0021, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)\nCosFaceModel_modi = CosFaceModel_combin(model_inputconv2)\nwith strategy.scope():        \n    #CosFaceModel_modi.compile(optimizer = opt, loss = cos_loss)\n    CosFaceModel_modi.compile(optimizer = opt, loss=cos_loss,  metrics= 'accuracy') #, metrics= my_metrics\nbasic_model.load_weights('/kaggle/input/tputraindata15/basic_model80.weights18.h5')\nCosFaceModel.load_weights('/kaggle/input/tputraindata15/CosFaceModelmodi80.weights18.h5')  \nmodevgg16_gen_20_6.load_weights('/kaggle/input/tputraindata15/face_CNN_model_finalvgg'+str(56))","metadata":{"execution":{"iopub.status.busy":"2024-04-02T07:01:00.369180Z","iopub.execute_input":"2024-04-02T07:01:00.369920Z","iopub.status.idle":"2024-04-02T07:01:13.716087Z","shell.execute_reply.started":"2024-04-02T07:01:00.369884Z","shell.execute_reply":"2024-04-02T07:01:13.714981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.load_weights('/kaggle/input/tputraindata15/basic_model80.weights18.h5')\nCosFaceModel.load_weights('/kaggle/input/tputraindata15/CosFaceModelmodi80.weights18.h5')  \nmodevgg16_gen_20_6.load_weights('/kaggle/input/tputraindata15/face_CNN_model_finalvgg'+str(56))","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:25:19.499165Z","iopub.execute_input":"2024-04-03T01:25:19.500279Z","iopub.status.idle":"2024-04-03T01:25:33.038119Z","shell.execute_reply.started":"2024-04-03T01:25:19.500240Z","shell.execute_reply":"2024-04-03T01:25:33.036907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print( modevgg16_gen_20_6.trainable)\nprint( CosFaceModel.trainable)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:22:54.764667Z","iopub.execute_input":"2024-04-03T01:22:54.765793Z","iopub.status.idle":"2024-04-03T01:22:54.770621Z","shell.execute_reply.started":"2024-04-03T01:22:54.765745Z","shell.execute_reply":"2024-04-03T01:22:54.769647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#gen_loss1, _, _, _ ,_,_= c_AB.train_on_batch([X_realA, X_realB], [y_realA, X_realB, X_realA, X_realB,yreal2])\nfor idx, (image,imagetarget, label) in enumerate(train_ds.take(steps_per_epoch)):\n    print(image.shape,imagetarget.shape)\n    lossimage,losscos = CosFaceModel_modi.train_on_batch([image],[imagetarget, label])\n    print(lossimage,losscos)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 12x12 to 80x80 model 80x80\nmodevgg16_gen_20_7.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-04-03T03:14:58.585169Z","iopub.execute_input":"2024-04-03T03:14:58.586075Z","iopub.status.idle":"2024-04-03T03:14:59.156394Z","shell.execute_reply.started":"2024-04-03T03:14:58.586033Z","shell.execute_reply":"2024-04-03T03:14:59.155170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 12x12 to 80x80 model 80x80\nmodevgg16_gen_20_7.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-04-03T01:32:16.226371Z","iopub.execute_input":"2024-04-03T01:32:16.226806Z","iopub.status.idle":"2024-04-03T02:49:40.438945Z","shell.execute_reply.started":"2024-04-03T01:32:16.226769Z","shell.execute_reply":"2024-04-03T02:49:40.437932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 12x12 to 80x80 model 80x80\nCosFaceModel_modi.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:41:21.871037Z","iopub.execute_input":"2024-04-02T04:41:21.872142Z","iopub.status.idle":"2024-04-02T05:26:33.309744Z","shell.execute_reply.started":"2024-04-02T04:41:21.872101Z","shell.execute_reply":"2024-04-02T05:26:33.308694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#80x80 run 12x12 to 80x80 model 80x80\nCosFaceModel_modi.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:51:08.620991Z","iopub.execute_input":"2024-04-02T03:51:08.621975Z","iopub.status.idle":"2024-04-02T04:36:03.584648Z","shell.execute_reply.started":"2024-04-02T03:51:08.621933Z","shell.execute_reply":"2024-04-02T04:36:03.583432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#80x80 run 28x28 to 80x80 model 80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:27:23.295707Z","iopub.execute_input":"2024-04-02T03:27:23.296139Z","iopub.status.idle":"2024-04-02T03:46:21.121960Z","shell.execute_reply.started":"2024-04-02T03:27:23.296103Z","shell.execute_reply":"2024-04-02T03:46:21.120789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#80x80 run double model 80x80\nCosFaceModel_modi.fit(\n            train_ds,\n            epochs=6,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-04-02T01:23:41.671007Z","iopub.execute_input":"2024-04-02T01:23:41.671423Z","iopub.status.idle":"2024-04-02T02:21:29.446743Z","shell.execute_reply.started":"2024-04-02T01:23:41.671389Z","shell.execute_reply":"2024-04-02T02:21:29.445562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run double model 80x80\nCosFaceModel_modi.fit(\n            train_ds,\n            epochs=9,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-04-01T11:51:09.806033Z","iopub.status.idle":"2024-04-01T11:51:09.806494Z","shell.execute_reply.started":"2024-04-01T11:51:09.806270Z","shell.execute_reply":"2024-04-01T11:51:09.806290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel2\n#80x80 run 32x32->80x80\nCosFaceModel2.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T05:08:26.287377Z","iopub.execute_input":"2024-03-24T05:08:26.287774Z","iopub.status.idle":"2024-03-24T05:20:57.021087Z","shell.execute_reply.started":"2024-03-24T05:08:26.287743Z","shell.execute_reply":"2024-03-24T05:20:57.01993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 32x32->80x80\nCosFaceModel2.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T05:21:54.838967Z","iopub.execute_input":"2024-03-24T05:21:54.839417Z","iopub.status.idle":"2024-03-24T05:39:20.02665Z","shell.execute_reply.started":"2024-03-24T05:21:54.839382Z","shell.execute_reply":"2024-03-24T05:39:20.02551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T05:40:55.89048Z","iopub.execute_input":"2024-03-24T05:40:55.890886Z","iopub.status.idle":"2024-03-24T05:53:29.119292Z","shell.execute_reply.started":"2024-03-24T05:40:55.890843Z","shell.execute_reply":"2024-03-24T05:53:29.117934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T06:51:25.105753Z","iopub.execute_input":"2024-03-24T06:51:25.106096Z","iopub.status.idle":"2024-03-24T07:03:07.934093Z","shell.execute_reply.started":"2024-03-24T06:51:25.106066Z","shell.execute_reply":"2024-03-24T07:03:07.932926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T07:06:23.789486Z","iopub.execute_input":"2024-03-24T07:06:23.790236Z","iopub.status.idle":"2024-03-24T07:13:08.006856Z","shell.execute_reply.started":"2024-03-24T07:06:23.790198Z","shell.execute_reply":"2024-03-24T07:13:08.005747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T07:13:24.945256Z","iopub.execute_input":"2024-03-24T07:13:24.945689Z","iopub.status.idle":"2024-03-24T07:19:23.150655Z","shell.execute_reply.started":"2024-03-24T07:13:24.945654Z","shell.execute_reply":"2024-03-24T07:19:23.149654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T03:33:20.234108Z","iopub.execute_input":"2024-03-24T03:33:20.235171Z","iopub.status.idle":"2024-03-24T03:40:14.975623Z","shell.execute_reply.started":"2024-03-24T03:33:20.235129Z","shell.execute_reply":"2024-03-24T03:40:14.97462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 2 80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T03:50:51.989791Z","iopub.execute_input":"2024-03-24T03:50:51.990205Z","iopub.status.idle":"2024-03-24T03:56:55.19741Z","shell.execute_reply.started":"2024-03-24T03:50:51.990171Z","shell.execute_reply":"2024-03-24T03:56:55.196369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 2 80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T03:59:37.792036Z","iopub.execute_input":"2024-03-24T03:59:37.792506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 2 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-23T01:31:00.189534Z","iopub.execute_input":"2024-03-23T01:31:00.190411Z","iopub.status.idle":"2024-03-23T01:41:28.467754Z","shell.execute_reply.started":"2024-03-23T01:31:00.190372Z","shell.execute_reply":"2024-03-23T01:41:28.466731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 2 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-23T01:48:32.842145Z","iopub.execute_input":"2024-03-23T01:48:32.842539Z","iopub.status.idle":"2024-03-23T01:58:12.893981Z","shell.execute_reply.started":"2024-03-23T01:48:32.842506Z","shell.execute_reply":"2024-03-23T01:58:12.892608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 2 32x32->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-23T02:02:50.918395Z","iopub.execute_input":"2024-03-23T02:02:50.918889Z","iopub.status.idle":"2024-03-23T02:07:40.643341Z","shell.execute_reply.started":"2024-03-23T02:02:50.918849Z","shell.execute_reply":"2024-03-23T02:07:40.642332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 first run \nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T03:03:30.294621Z","iopub.execute_input":"2024-03-24T03:03:30.29558Z","iopub.status.idle":"2024-03-24T03:23:18.69572Z","shell.execute_reply.started":"2024-03-24T03:03:30.295538Z","shell.execute_reply":"2024-03-24T03:23:18.694631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 first run 2\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-23T02:45:34.248755Z","iopub.execute_input":"2024-03-23T02:45:34.249664Z","iopub.status.idle":"2024-03-23T03:02:36.312481Z","shell.execute_reply.started":"2024-03-23T02:45:34.249624Z","shell.execute_reply":"2024-03-23T03:02:36.311438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 first run 3\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-23T03:12:13.35407Z","iopub.execute_input":"2024-03-23T03:12:13.354482Z","iopub.status.idle":"2024-03-23T03:29:22.451226Z","shell.execute_reply.started":"2024-03-23T03:12:13.354446Z","shell.execute_reply":"2024-03-23T03:29:22.450105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#160x160 first run 1\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-24T02:39:16.412191Z","iopub.execute_input":"2024-03-24T02:39:16.412647Z","iopub.status.idle":"2024-03-24T02:54:57.221244Z","shell.execute_reply.started":"2024-03-24T02:39:16.412607Z","shell.execute_reply":"2024-03-24T02:54:57.220041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#160x160 first run 2\nCosFaceModel.fit(\n            train_ds,\n            epochs=1,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 first run  128\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T01:24:49.517094Z","iopub.execute_input":"2024-03-25T01:24:49.517437Z","iopub.status.idle":"2024-03-25T01:44:20.776553Z","shell.execute_reply.started":"2024-03-25T01:24:49.517407Z","shell.execute_reply":"2024-03-25T01:44:20.775653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 second run  128\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T01:45:09.033806Z","iopub.execute_input":"2024-03-25T01:45:09.034291Z","iopub.status.idle":"2024-03-25T02:03:43.085134Z","shell.execute_reply.started":"2024-03-25T01:45:09.034251Z","shell.execute_reply":"2024-03-25T02:03:43.084174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 20x20->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T01:42:43.094055Z","iopub.execute_input":"2024-03-31T01:42:43.094482Z","iopub.status.idle":"2024-03-31T01:55:12.523542Z","shell.execute_reply.started":"2024-03-31T01:42:43.094447Z","shell.execute_reply":"2024-03-31T01:55:12.522381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 20x20->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T01:56:10.545716Z","iopub.execute_input":"2024-03-31T01:56:10.546149Z","iopub.status.idle":"2024-03-31T02:13:33.385400Z","shell.execute_reply.started":"2024-03-31T01:56:10.546113Z","shell.execute_reply":"2024-03-31T02:13:33.384391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=.000211, beta_1=0.9, beta_2=0.999, epsilon=0.1) # tfa.optimizers.SGDW(learning_rate=0.1, momentum=0.9, weight_decay=5e-5)  #.0051\ncos_loss = CosfaceLoss(num_classes = 85742)\nwith strategy.scope():\n    basic_model = build_embedding3()\n    inputs = basic_model.inputs[0] # Model may have multiple inputs\n    embeddings = basic_model.outputs[0] # Model may have multiple outputs\n    classification_output_layer = NormDense(units=num_classes)(embeddings)\n    \n    CosFaceModel = Model(inputs, classification_output_layer)\n    \n    CosFaceModel.compile(optimizer = opt, loss = cos_loss) #, metrics = [\"accuracy\"]","metadata":{"execution":{"iopub.status.busy":"2024-04-01T13:19:34.302139Z","iopub.execute_input":"2024-04-01T13:19:34.303296Z","iopub.status.idle":"2024-04-01T13:19:35.427497Z","shell.execute_reply.started":"2024-04-01T13:19:34.303255Z","shell.execute_reply":"2024-04-01T13:19:35.426442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T07:17:20.687878Z","iopub.execute_input":"2024-03-31T07:17:20.688289Z","iopub.status.idle":"2024-03-31T07:29:50.410215Z","shell.execute_reply.started":"2024-03-31T07:17:20.688254Z","shell.execute_reply":"2024-03-31T07:29:50.409202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T06:59:43.096372Z","iopub.execute_input":"2024-03-31T06:59:43.096763Z","iopub.status.idle":"2024-03-31T07:12:14.612640Z","shell.execute_reply.started":"2024-03-31T06:59:43.096729Z","shell.execute_reply":"2024-03-31T07:12:14.611435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T06:35:31.108188Z","iopub.execute_input":"2024-03-31T06:35:31.108600Z","iopub.status.idle":"2024-03-31T06:53:55.025648Z","shell.execute_reply.started":"2024-03-31T06:35:31.108565Z","shell.execute_reply":"2024-03-31T06:53:55.024554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 28x28->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T05:55:26.132021Z","iopub.execute_input":"2024-03-31T05:55:26.132416Z","iopub.status.idle":"2024-03-31T06:13:43.277389Z","shell.execute_reply.started":"2024-03-31T05:55:26.132383Z","shell.execute_reply":"2024-03-31T06:13:43.276135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 20x20->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T02:17:06.314069Z","iopub.execute_input":"2024-03-31T02:17:06.314504Z","iopub.status.idle":"2024-03-31T02:35:27.033315Z","shell.execute_reply.started":"2024-03-31T02:17:06.314467Z","shell.execute_reply":"2024-03-31T02:35:27.032279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T02:50:19.944352Z","iopub.execute_input":"2024-03-31T02:50:19.944722Z","iopub.status.idle":"2024-03-31T03:08:39.160438Z","shell.execute_reply.started":"2024-03-31T02:50:19.944690Z","shell.execute_reply":"2024-03-31T03:08:39.159334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T03:26:39.318109Z","iopub.execute_input":"2024-03-31T03:26:39.319330Z","iopub.status.idle":"2024-03-31T03:45:08.155492Z","shell.execute_reply.started":"2024-03-31T03:26:39.319281Z","shell.execute_reply":"2024-03-31T03:45:08.154388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T03:46:22.095108Z","iopub.execute_input":"2024-03-31T03:46:22.095488Z","iopub.status.idle":"2024-03-31T04:04:58.258366Z","shell.execute_reply.started":"2024-03-31T03:46:22.095454Z","shell.execute_reply":"2024-03-31T04:04:58.257421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T04:16:33.249451Z","iopub.execute_input":"2024-03-31T04:16:33.250527Z","iopub.status.idle":"2024-03-31T04:35:07.758950Z","shell.execute_reply.started":"2024-03-31T04:16:33.250487Z","shell.execute_reply":"2024-03-31T04:35:07.757760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 run 26x26->80x80\nCosFaceModel.fit(\n            train_ds,\n            epochs=3,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-31T04:39:34.390700Z","iopub.execute_input":"2024-03-31T04:39:34.391135Z","iopub.status.idle":"2024-03-31T04:57:25.204919Z","shell.execute_reply.started":"2024-03-31T04:39:34.391099Z","shell.execute_reply":"2024-03-31T04:57:25.203730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 third run  128\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T02:06:15.550989Z","iopub.execute_input":"2024-03-25T02:06:15.552065Z","iopub.status.idle":"2024-03-25T02:24:57.248513Z","shell.execute_reply.started":"2024-03-25T02:06:15.552001Z","shell.execute_reply":"2024-03-25T02:24:57.247345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 third run  128\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T02:26:01.647498Z","iopub.execute_input":"2024-03-25T02:26:01.648357Z","iopub.status.idle":"2024-03-25T02:45:28.160919Z","shell.execute_reply.started":"2024-03-25T02:26:01.648319Z","shell.execute_reply":"2024-03-25T02:45:28.159909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#112x112 third run  128\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T02:51:39.208364Z","iopub.execute_input":"2024-03-25T02:51:39.208686Z","iopub.status.idle":"2024-03-25T03:11:08.013299Z","shell.execute_reply.started":"2024-03-25T02:51:39.208658Z","shell.execute_reply":"2024-03-25T03:11:08.012028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 third run  128  112->80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T03:19:08.532166Z","iopub.execute_input":"2024-03-25T03:19:08.532566Z","iopub.status.idle":"2024-03-25T03:31:51.489917Z","shell.execute_reply.started":"2024-03-25T03:19:08.532532Z","shell.execute_reply":"2024-03-25T03:31:51.488767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 third run  128 80->112\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T03:59:42.491766Z","iopub.execute_input":"2024-03-25T03:59:42.492174Z","iopub.status.idle":"2024-03-25T04:18:28.465931Z","shell.execute_reply.started":"2024-03-25T03:59:42.492146Z","shell.execute_reply":"2024-03-25T04:18:28.464905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 third run  128 112->80\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T04:32:29.128525Z","iopub.execute_input":"2024-03-25T04:32:29.129011Z","iopub.status.idle":"2024-03-25T04:44:16.396996Z","shell.execute_reply.started":"2024-03-25T04:32:29.128951Z","shell.execute_reply":"2024-03-25T04:44:16.395828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#80x80 third run  128 80->112\nCosFaceModel.fit(\n            train_ds,\n            epochs=2,\n            callbacks=callbacks,\n            steps_per_epoch=steps_per_epoch)  #num of batches","metadata":{"execution":{"iopub.status.busy":"2024-03-25T04:57:30.028555Z","iopub.execute_input":"2024-03-25T04:57:30.028992Z","iopub.status.idle":"2024-03-25T05:16:14.408049Z","shell.execute_reply.started":"2024-03-25T04:57:30.028954Z","shell.execute_reply":"2024-03-25T05:16:14.407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.load_weights('/kaggle/input/tputraindata7/CosFaceModelmodi80.weights6.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T10:11:11.545886Z","iopub.execute_input":"2024-03-18T10:11:11.546254Z","iopub.status.idle":"2024-03-18T10:11:20.665334Z","shell.execute_reply.started":"2024-03-18T10:11:11.546223Z","shell.execute_reply":"2024-03-18T10:11:20.664478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.save_weights('CosFaceModelmodi.weights3.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:50:52.180808Z","iopub.execute_input":"2024-03-18T02:50:52.181184Z","iopub.status.idle":"2024-03-18T02:50:53.597042Z","shell.execute_reply.started":"2024-03-18T02:50:52.18115Z","shell.execute_reply":"2024-03-18T02:50:53.596124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.load_weights('/kaggle/input/tputraindata3/arcFaceModelOrign.weights.h5')  ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.load_weights('CosFaceModelmodi.weights3.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:51:03.351227Z","iopub.execute_input":"2024-03-18T02:51:03.351558Z","iopub.status.idle":"2024-03-18T02:51:10.720574Z","shell.execute_reply.started":"2024-03-18T02:51:03.351531Z","shell.execute_reply":"2024-03-18T02:51:10.719583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.save_weights('basic_model.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T02:50:59.444466Z","iopub.execute_input":"2024-03-18T02:50:59.445428Z","iopub.status.idle":"2024-03-18T02:51:00.545405Z","shell.execute_reply.started":"2024-03-18T02:50:59.445388Z","shell.execute_reply":"2024-03-18T02:51:00.54444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.save_weights('CosFaceModelmodi80.weights8.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-19T02:03:16.742586Z","iopub.execute_input":"2024-03-19T02:03:16.743657Z","iopub.status.idle":"2024-03-19T02:03:18.157291Z","shell.execute_reply.started":"2024-03-19T02:03:16.743618Z","shell.execute_reply":"2024-03-19T02:03:18.155973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.save_weights('basic_model80.weights8.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-19T02:03:14.09285Z","iopub.execute_input":"2024-03-19T02:03:14.093539Z","iopub.status.idle":"2024-03-19T02:03:15.0197Z","shell.execute_reply.started":"2024-03-19T02:03:14.093501Z","shell.execute_reply":"2024-03-19T02:03:15.018448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.load_weights('CosFaceModelmodi80.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T04:42:22.864319Z","iopub.execute_input":"2024-03-18T04:42:22.864756Z","iopub.status.idle":"2024-03-18T04:42:50.417497Z","shell.execute_reply.started":"2024-03-18T04:42:22.864651Z","shell.execute_reply":"2024-03-18T04:42:50.416368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.save_weights('basic_model160.weights6.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:09:20.966885Z","iopub.execute_input":"2024-03-18T11:09:20.967362Z","iopub.status.idle":"2024-03-18T11:09:21.936712Z","shell.execute_reply.started":"2024-03-18T11:09:20.967244Z","shell.execute_reply":"2024-03-18T11:09:21.935675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.save_weights('CosFaceModelmodi160.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:09:36.63514Z","iopub.execute_input":"2024-03-18T11:09:36.63593Z","iopub.status.idle":"2024-03-18T11:09:46.069433Z","shell.execute_reply.started":"2024-03-18T11:09:36.635889Z","shell.execute_reply":"2024-03-18T11:09:46.06811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.save_weights('basic_model160_80.weights6.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:33:28.447023Z","iopub.execute_input":"2024-03-18T11:33:28.448121Z","iopub.status.idle":"2024-03-18T11:33:29.437162Z","shell.execute_reply.started":"2024-03-18T11:33:28.448083Z","shell.execute_reply":"2024-03-18T11:33:29.436002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.save_weights('CosFaceModelmodi160_80.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:33:30.907491Z","iopub.execute_input":"2024-03-18T11:33:30.907918Z","iopub.status.idle":"2024-03-18T11:33:40.271708Z","shell.execute_reply.started":"2024-03-18T11:33:30.907887Z","shell.execute_reply":"2024-03-18T11:33:40.270517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_model.load_weights('basic_model160_80.weights6.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:37:57.928411Z","iopub.execute_input":"2024-03-18T11:37:57.929479Z","iopub.status.idle":"2024-03-18T11:38:01.959673Z","shell.execute_reply.started":"2024-03-18T11:37:57.929442Z","shell.execute_reply":"2024-03-18T11:38:01.958691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.load_weights('CosFaceModelmodi160_80.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:38:16.430051Z","iopub.execute_input":"2024-03-18T11:38:16.430311Z","iopub.status.idle":"2024-03-18T11:38:31.022785Z","shell.execute_reply.started":"2024-03-18T11:38:16.430284Z","shell.execute_reply":"2024-03-18T11:38:31.021587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CosFaceModel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-18T11:18:34.702444Z","iopub.execute_input":"2024-03-18T11:18:34.703088Z","iopub.status.idle":"2024-03-18T11:18:34.931802Z","shell.execute_reply.started":"2024-03-18T11:18:34.703048Z","shell.execute_reply":"2024-03-18T11:18:34.930796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"make data","metadata":{}},{"cell_type":"code","source":"\ndef save_tfrecords(dense_fes, sparse_fes, seq_fes, dest_file):  \n    \"\"\" \n    Args:         \n        sparse_fes: 要保存到TFRecord文件的第2个numpy array，用 Int64List 来存储。 \n        seq_fes: 要保存到TFRecord文件的第3个numpy array，用 BytesList。         \n        dest_file: 输出文件的路径。 \n    Returns: \n        不返回任何值 \n    \"\"\"  \n    len_all = sparse_fes.shape[0]# 总体样本量\n    with tf.io.TFRecordWriter(dest_file) as writer:\n        for index in range(len_all):\n           \n            sparse_features = sparse_fes[index,:]\n            seq_features = seq_fes[index,:]\n           \n            features = tf.train.Features(  \n                feature={  \n                    \"seq_features\": tf.train.Feature(  \n                        bytes_list=tf.train.BytesList(value=[seq_features.tobytes()])),  \n                    \n                    \"sparse_features\": tf.train.Feature(  \n                        int64_list=tf.train.Int64List(value=list(sparse_features)))  \n                   \n                }  \n            )  \n            tf_example = tf.train.Example(features=features)  \n            serialized = tf_example.SerializeToString()\n            writer.write(serialized)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#celeb model ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt \nimport seaborn as sns\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nimport os\nimport math \nimport cv2\nimport re\nfrom tqdm import tqdm\n\nsns.set_style('dark')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/celeba-face-recognition-triplets/CelebA FR Triplets/CelebA FR Triplets/triplets.csv\")\n#data = pd.read_csv(\"/kaggle/input/my-saimese-nw-data/ppD_v4/siamese_nw_data.csv\")\ndata = data.sample(frac=1,random_state=42).reset_index(drop=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.nunique()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = data.head(16332-2000)\ntest = data.tail(2000)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom tensorflow.keras.layers import Activation, Dropout, Flatten, Dense, Input, Layer, Lambda, BatchNormalization \nfrom tensorflow.keras.applications import ResNet50, VGG16, InceptionV3, MobileNetV2, EfficientNetB3\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport math\nimport cv2\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport PIL\nfrom sklearn.preprocessing import normalize # for normalizing np array to avoid NaN from dividing by 0\nimport glob\n\n# Import tensorflow dependencies - Functional API\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Input, Dropout, BatchNormalization","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = 160\ndef build_embedding():\n    #base_model = tf.keras.applications.EfficientNetV2B0(include_top=False, weights=\"imagenet\", input_shape=(80, 80, 3))\n    base_model = tf.keras.applications.xception.Xception(include_top=False, weights=\"imagenet\", input_shape=(size, size, 3))\n#     base_model.trainable = False # Freeze the model\n    \n#     # Unfreeze the top 20 layers while leaving BatchNorm layers frozen\n#     for layer in base_model.layers[-20:]:\n#         if not isinstance(layer, BatchNormalization):\n#             layer.trainable = True\n    \n    avg_pool = GlobalAveragePooling2D(name=\"avg_pool\")(base_model.output)  # Pass the output tensor of base_model to GlobalAveragePooling2D\n    \n    top_dropout_rate = 0.2\n    top_dropout = Dropout(top_dropout_rate, name=\"top_dropout\")(avg_pool)\n    \n    dense_512 = Dense(units=512, activation=None, name=\"pred\")(top_dropout)  # No activation on the final dense layer == linear activation\n    embedding = BatchNormalization(momentum=.99, epsilon=.001, scale=True, name=\"embedding\")(dense_512)\n    # build the embedding model and return it\n    return Model(base_model.input, embedding, name=\"embedding\")\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings = build_embedding()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings.load_weights('/kaggle/input/facedata1/basic_model.weights.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in embeddings.layers[-10:]: \n#    if layer.name == 'Block8_3_ScaleSum':\n#        break\n    layer.trainable = True\nfor layer in embeddings.layers[:-10]: \n#    if layer.name == 'Block8_3_ScaleSum':\n#        break\n    layer.trainable = False\nfor layer in embeddings.layers: \n#    if layer.name == 'Block8_3_ScaleSum':\n#        break\n    print(layer.name,layer.trainable)\n    #layer.trainable = False","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_input1 = Input(shape=(img_size,img_size,3),name='Image1')\nimage_input2 = Input(shape=(img_size,img_size,3),name='Image2')\nimage_input3 = Input(shape=(img_size,img_size,3),name='Image3')\n\nanchor = embeddings(image_input1)\npositive = embeddings(image_input2)\nnegative = embeddings(image_input3)\n\nsiamese_network = Model(inputs=[image_input1,image_input2,image_input3], outputs=[anchor,positive,negative])\nsiamese_network.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\ndatagen = ImageDataGenerator(rescale=1/255.,horizontal_flip=True,rotation_range=6)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TripleGenerator(tf.keras.utils.Sequence):\n    \n    def __init__(self, gen1, gen2, gen3):\n        \n        self.gen1 = gen1\n        self.gen2 = gen2\n        self.gen3 = gen3\n\n    def __len__(self):\n        \n        return len(self.gen1)\n\n    def __getitem__(self, i):\n        \n        x1 = self.gen1[i]\n        x2 = self.gen2[i]\n        x3 = self.gen3[i]\n        \n        return [x1,x2,x3]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#directory = '/kaggle/input/my-saimese-nw-data/ppD_v4/saimese_data'\ndirectory = '/kaggle/input/celeba-face-recognition-triplets/CelebA FR Triplets/CelebA FR Triplets/images'\n#anchor\tid1\tpos\tid2\tneg\tid3\ndef create_generator(folder,dataset,column):\n    generator = datagen.flow_from_dataframe(dataframe=dataset,\n                                            directory=folder,\n                                            x_col=column,\n                                            target_size=(img_size, img_size),\n                                            batch_size=batch_size,\n                                            class_mode=None,\n                                            shuffle=False)\n    return generator\n\ntrain_generator1 = create_generator(directory,train,'anchor')\ntrain_generator2 = create_generator(directory,train,'pos')\ntrain_generator3 = create_generator(directory,train,'neg')\n\ntest_generator1 = create_generator(directory,test,'anchor')\ntest_generator2 = create_generator(directory,test,'pos')\ntest_generator3 = create_generator(directory,test,'neg')\n\n# train_generator1 = create_generator(directory,train,'anchor')\n# train_generator2 = create_generator(directory,train,'pos')\n# train_generator3 = create_generator(directory,train,'neg')\n\n# test_generator1 = create_generator(directory,test,'anchor')\n# test_generator2 = create_generator(directory,test,'pos')\n# test_generator3 = create_generator(directory,test,'neg')\n\ntrain_generator = TripleGenerator(train_generator1,train_generator2,train_generator3)\ntest_generator = TripleGenerator(test_generator1,test_generator2,test_generator3)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples = train_generator[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, axarr = plt.subplots(10,3,figsize=(20, 60))\n\nfor i in range(0,10):\n\n    axarr[i,0].imshow(samples[0][i])\n    axarr[i,0].title.set_text('Anchor Image')\n    axarr[i,0].axis('off')\n    axarr[i,1].imshow(samples[1][i])\n    axarr[i,1].title.set_text('Positive Image')\n    axarr[i,1].axis('off')\n    axarr[i,2].imshow(samples[2][i])\n    axarr[i,2].title.set_text('Negative Image')\n    axarr[i,2].axis('off')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(\n    siamese_network,\n    to_file='model.png')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path = \"model.h5\"\ncheckpoint = ModelCheckpoint(model_path,\n                             monitor=\"val_loss\",\n                             mode=\"min\",\n                             save_best_only = True,\n                             verbose=1,\n                             save_weights_only=True)\n\nearlystop = EarlyStopping(monitor = 'val_loss', \n                          min_delta = 0, \n                          patience = 5,\n                          verbose = 1,\n                          restore_best_weights = True)\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', \n                                            patience=4, \n                                            verbose=1, \n                                            factor=0.3, \n                                            min_lr=0.00000001)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import losses\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras import metrics","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SiameseModel(Model):\n    \n    def __init__(self, siamese_network, margin=0.5):\n        super().__init__()\n        self.siamese_network = siamese_network\n        self.margin = margin\n        self.loss_tracker = metrics.Mean(name='loss')\n        \n    def call(self, inputs):\n        return self.siamese_network(inputs)\n    \n    def train_step(self, data):\n        with tf.GradientTape() as tape:\n            loss = self._compute_loss(data)\n        gradients = tape.gradient(loss,self.siamese_network.trainable_weights)\n        self.optimizer.apply_gradients(\n                    zip(gradients,self.siamese_network.trainable_weights)\n        )\n        \n        self.loss_tracker.update_state(loss)\n        return {\"loss\": self.loss_tracker.result()}\n    \n    def test_step(self, data):\n        loss = self._compute_loss(data)\n        self.loss_tracker.update_state(loss)\n        return {\"loss\": self.loss_tracker.result()}\n    \n    def _compute_loss(self, data):\n        anchor, positive, negative = self.siamese_network(data)\n        pos_dist = tf.reduce_sum(tf.square(anchor-positive), -1)\n        neg_dist = tf.reduce_sum(tf.square(anchor-negative), -1)\n        loss = pos_dist - neg_dist + self.margin\n        loss = tf.maximum(loss, 0.0)\n        return loss \n    \n    @property\n    def metrics(self):\n        return [self.loss_tracker]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"siamese_model = SiameseModel(siamese_network)\nsiamese_model.compile(optimizer=optimizers.Adam(learning_rate=0.0001))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics.pairwise import euclidean_distances as L2","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    history = siamese_model.fit(train_generator, validation_data=test_generator,\n                            epochs=9,callbacks=[checkpoint\n                                                 ,earlystop,learning_rate_reduction]) \nexcept KeyboardInterrupt:\n    print(\"\\nTraining Stopped!\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}