{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.15","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":21154,"databundleVersionId":1243559,"sourceType":"competition"},{"sourceId":5495546,"sourceType":"datasetVersion","datasetId":3171191}],"dockerImageVersionId":30806,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"![](https://i.postimg.cc/WpSHNwwC/Screenshot-8.png)","metadata":{}},{"cell_type":"code","source":"\"\"\"\nPython 3.10 predict_models_DenseNet201\nFile name DenseNet201.py\n\nVersion: 0.1\nAuthor: MLCV\nDate: 2023-12-15\n\"\"\"\n\nimport tensorflow as tf\nfrom tensorflow.keras.applications import DenseNet201 \nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.layers import Dropout, BatchNormalization, GaussianDropout\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nfrom tensorflow.keras.callbacks import ModelCheckpoint\n\nfrom kaggle_datasets import KaggleDatasets\nimport re\nimport numpy as np\nimport random\nimport matplotlib.pyplot as plt\n%matplotlib inline \nprint(\"Tensorflow version \" + tf.__version__)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:54:07.271316Z","iopub.execute_input":"2024-12-09T19:54:07.271684Z","iopub.status.idle":"2024-12-09T19:54:22.055646Z","shell.execute_reply.started":"2024-12-09T19:54:07.271633Z","shell.execute_reply":"2024-12-09T19:54:22.054935Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\n# Detect hardware, return appropriate distribution strategy: TPU, GPU, CPU\n# Detection of hardware, returning the appropriate distribution strategy: TPU, GPU, CPU\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # Detecting TPU. Environment parameters are not required if the TPU_NAME environment variable is set. On Kaggle, this is always the case.\n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy() # Default distribution strategy in TensorFlow. Works on CPU and a single GPU.\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:54:22.056877Z","iopub.execute_input":"2024-12-09T19:54:22.057352Z","iopub.status.idle":"2024-12-09T19:54:30.328232Z","shell.execute_reply.started":"2024-12-09T19:54:22.05732Z","shell.execute_reply":"2024-12-09T19:54:30.327311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"GCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')\n# GCS_DS_PATH_EXT = KaggleDatasets().get_gcs_path('tf-flower-photo-tfrec')\nIMAGE_SIZE = [512, 512] # At this size, the GPU will run out of memory. Use TPU instead.\n# EPOCHS = 35\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\n# External data\nGCS_PATH_SELECT_EXT = {\n    192: '/tfrecords-jpeg-192x192',\n    224: '/tfrecords-jpeg-224x224',\n    331: '/tfrecords-jpeg-331x331',\n    512: '/tfrecords-jpeg-512x512'\n}\nGCS_PATH_EXT = GCS_PATH_SELECT_EXT[IMAGE_SIZE[0]]\n\n# IMAGENET_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/imagenet' + GCS_PATH_EXT + '/*.tfrec')\n# INATURELIST_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/inaturalist' + GCS_PATH_EXT + '/*.tfrec')\n# OPENIMAGE_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/openimage' + GCS_PATH_EXT + '/*.tfrec')\n# OXFORD_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/oxford_102' + GCS_PATH_EXT + '/*.tfrec')\n# TENSORFLOW_FILES = tf.io.gfile.glob(GCS_DS_PATH_EXT + '/tf_flowers' + GCS_PATH_EXT + '/*.tfrec')\n\n# ADDITIONAL_TRAINING_FILENAMES = IMAGENET_FILES + INATURELIST_FILES + OPENIMAGE_FILES + OXFORD_FILES + TENSORFLOW_FILES  \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\n\n# TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')\n# VALIDATION_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\n# TRAINING_FILENAMES = TRAINING_FILENAMES + ADDITIONAL_TRAINING_FILENAMES","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:54:30.329266Z","iopub.execute_input":"2024-12-09T19:54:30.329544Z","iopub.status.idle":"2024-12-09T19:54:30.346992Z","shell.execute_reply.started":"2024-12-09T19:54:30.329515Z","shell.execute_reply":"2024-12-09T19:54:30.346312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = tf.keras.models.load_model('/kaggle/input/densenet201-petals/my_denceNet_201.h5')\n\ndef decode_image(image_data):\n    \"\"\"Decodes an image into a tensor. Normalizes the data and resizes the image to the specified size.\"\"\"\n    image = tf.image.decode_jpeg(image_data, channels=3) # Decodes a JPEG image into a uint8 tensor.\n    image = tf.cast(image, tf.float32) / 255.0  # Converts the image to float in the range [0, 1].\n    image = tf.reshape(image, [*IMAGE_SIZE, 3]) # Explicit size, necessary for TPU.\n#     image = tf.keras.applications.inception_resnet_v2.preprocess_input(image)\n    return image\n\ndef load_dataset(filenames, labeled=True, ordered=False):\n    \"\"\"Reads from TFRecords. For optimal performance, multiple files are read simultaneously without regard to the order of the data. The order does not matter because we will shuffle the data anyway.\"\"\"\n\n    ignore_order = tf.data.Options() # Represents options for tf.data.Dataset.\n    if not ordered:\n        ignore_order.experimental_deterministic = False # Disable ordering to increase speed.\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # Automatically interleaves reading from multiple files.\n    dataset = dataset.with_options(ignore_order) # Uses data as soon as it is read, not in the 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 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) # Prepares the next batch while the current one is being processed.\n    return dataset\n\ndef read_unlabeled_tfrecord(example):\n    UNLABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means a byte string.\n        \"id\": tf.io.FixedLenFeature([], tf.string),  # [] means a scalar element.\n        # No class present; the task of this competition is to predict the flower classes for the test dataset.\n    }\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n    image = decode_image(example['image']) # Convert the image to the desired format.\n    idnum = example['id']\n    return image, idnum # Returns a dataset of image(s).\n\ndef count_data_items(filenames):\n    # The number of data items is written in the name of the .tfrec files, e.g., 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\n# training_dataset = get_training_dataset()\n# validation_dataset = get_validation_dataset()\n\nNUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:54:30.348535Z","iopub.execute_input":"2024-12-09T19:54:30.34887Z","iopub.status.idle":"2024-12-09T19:54:31.363954Z","shell.execute_reply.started":"2024-12-09T19:54:30.348842Z","shell.execute_reply":"2024-12-09T19:54:31.362525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Since we are splitting the dataset and iterating separately for images and identifiers, the order matters.\ntest_ds = get_test_dataset(ordered=True) \n\nprint('Calculating predictions...')\ntest_images_ds = test_ds.map(lambda image, idnum: image)\nprobabilities = model.predict(test_images_ds)\npredictions = np.argmax(probabilities, axis=-1)\nprint(predictions)\n\nprint('Создание файла submission.csv...')\ntest_ids_ds = test_ds.map(lambda image, idnum: idnum).unbatch()\ntest_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES))).numpy().astype('U') # все в одной партии\nnp.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T19:54:31.364556Z","iopub.status.idle":"2024-12-09T19:54:31.36484Z","shell.execute_reply.started":"2024-12-09T19:54:31.364703Z","shell.execute_reply":"2024-12-09T19:54:31.364717Z"}},"outputs":[],"execution_count":null}]}