{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-01T06:02:49.914100Z","iopub.execute_input":"2023-06-01T06:02:49.914833Z","iopub.status.idle":"2023-06-01T06:03:07.774645Z","shell.execute_reply.started":"2023-06-01T06:02:49.914799Z","shell.execute_reply":"2023-06-01T06:03:07.770322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.applications import InceptionV3\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:49:08.982945Z","iopub.execute_input":"2023-06-06T18:49:08.984768Z","iopub.status.idle":"2023-06-06T18:49:18.288193Z","shell.execute_reply.started":"2023-06-06T18:49:08.984741Z","shell.execute_reply":"2023-06-06T18:49:18.287296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\ntrain_data = pd.read_csv('/kaggle/input/extracting-attributes-from-fashion-images-2/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:49:21.200638Z","iopub.execute_input":"2023-06-06T18:49:21.201827Z","iopub.status.idle":"2023-06-06T18:49:21.229073Z","shell.execute_reply.started":"2023-06-06T18:49:21.201792Z","shell.execute_reply":"2023-06-06T18:49:21.228218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the data into training and validation sets\ntrain_data, val_data = train_test_split(train_data, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:49:35.569844Z","iopub.execute_input":"2023-06-06T18:49:35.570283Z","iopub.status.idle":"2023-06-06T18:49:35.582249Z","shell.execute_reply.started":"2023-06-06T18:49:35.570241Z","shell.execute_reply":"2023-06-06T18:49:35.580478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:49:37.239518Z","iopub.execute_input":"2023-06-06T18:49:37.240372Z","iopub.status.idle":"2023-06-06T18:49:37.251944Z","shell.execute_reply.started":"2023-06-06T18:49:37.240323Z","shell.execute_reply":"2023-06-06T18:49:37.250950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define data augmentation and preprocessing\ntrain_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=0.2,\n    horizontal_flip=True,\n    brightness_range=[0.8, 1.2],\n    zoom_range=0.2\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:49:51.584844Z","iopub.execute_input":"2023-06-06T18:49:51.585185Z","iopub.status.idle":"2023-06-06T18:49:51.590160Z","shell.execute_reply.started":"2023-06-06T18:49:51.585157Z","shell.execute_reply":"2023-06-06T18:49:51.588937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:49:55.580242Z","iopub.execute_input":"2023-06-06T18:49:55.580584Z","iopub.status.idle":"2023-06-06T18:49:55.585309Z","shell.execute_reply.started":"2023-06-06T18:49:55.580555Z","shell.execute_reply":"2023-06-06T18:49:55.584028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert numeric labels to strings\ntrain_data['label'] = train_data['label'].astype(str)\n\n# Create the train generator\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_data,\n    x_col='file_name',\n    y_col='label',\n    directory='/kaggle/input/extracting-attributes-from-fashion-images-2/train',\n    shuffle=True,\n    target_size=(75, 75),\n    batch_size=32,\n    class_mode='sparse'\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:49:58.588086Z","iopub.execute_input":"2023-06-06T18:49:58.588426Z","iopub.status.idle":"2023-06-06T18:50:07.214465Z","shell.execute_reply.started":"2023-06-06T18:49:58.588398Z","shell.execute_reply":"2023-06-06T18:50:07.213558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert numeric labels to strings\nval_data['label'] = val_data['label'].astype(str)\n\nval_generator = val_datagen.flow_from_dataframe(\n    dataframe=val_data,\n    x_col=\"file_name\",\n    y_col=\"label\",\n    directory='/kaggle/input/extracting-attributes-from-fashion-images-2/train',\n    shuffle=True,\n    target_size=(75, 75), \n    batch_size=32,\n    class_mode='sparse'\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:09.348627Z","iopub.execute_input":"2023-06-06T18:50:09.349283Z","iopub.status.idle":"2023-06-06T18:50:12.382956Z","shell.execute_reply.started":"2023-06-06T18:50:09.349248Z","shell.execute_reply":"2023-06-06T18:50:12.381913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(75, 75, 3))","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:29.851940Z","iopub.execute_input":"2023-06-06T18:50:29.852286Z","iopub.status.idle":"2023-06-06T18:50:38.784322Z","shell.execute_reply.started":"2023-06-06T18:50:29.852259Z","shell.execute_reply":"2023-06-06T18:50:38.783366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:44.334649Z","iopub.execute_input":"2023-06-06T18:50:44.335566Z","iopub.status.idle":"2023-06-06T18:50:44.348494Z","shell.execute_reply.started":"2023-06-06T18:50:44.335531Z","shell.execute_reply":"2023-06-06T18:50:44.347566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:45.533399Z","iopub.execute_input":"2023-06-06T18:50:45.533775Z","iopub.status.idle":"2023-06-06T18:50:45.542511Z","shell.execute_reply.started":"2023-06-06T18:50:45.533743Z","shell.execute_reply":"2023-06-06T18:50:45.541524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes=7","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:48.784944Z","iopub.execute_input":"2023-06-06T18:50:48.785305Z","iopub.status.idle":"2023-06-06T18:50:48.789912Z","shell.execute_reply.started":"2023-06-06T18:50:48.785275Z","shell.execute_reply":"2023-06-06T18:50:48.788825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(1024, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.5)(x)\npredictions = Dense(num_classes, activation='softmax')(x)  # Replace 'num_classes' with the number of classes in your dataset\n\nmodel = Model(inputs=base_model.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:49.416628Z","iopub.execute_input":"2023-06-06T18:50:49.417294Z","iopub.status.idle":"2023-06-06T18:50:49.480020Z","shell.execute_reply.started":"2023-06-06T18:50:49.417260Z","shell.execute_reply":"2023-06-06T18:50:49.479136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:52.644934Z","iopub.execute_input":"2023-06-06T18:50:52.645335Z","iopub.status.idle":"2023-06-06T18:50:52.668877Z","shell.execute_reply.started":"2023-06-06T18:50:52.645300Z","shell.execute_reply":"2023-06-06T18:50:52.667118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:50:55.268038Z","iopub.execute_input":"2023-06-06T18:50:55.268383Z","iopub.status.idle":"2023-06-06T18:50:55.296867Z","shell.execute_reply.started":"2023-06-06T18:50:55.268355Z","shell.execute_reply":"2023-06-06T18:50:55.295884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_generator, epochs=20,validation_data=val_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T18:51:07.198601Z","iopub.execute_input":"2023-06-06T18:51:07.198986Z","iopub.status.idle":"2023-06-06T19:17:20.393179Z","shell.execute_reply.started":"2023-06-06T18:51:07.198956Z","shell.execute_reply":"2023-06-06T19:17:20.392019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(val_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T19:17:55.450528Z","iopub.execute_input":"2023-06-06T19:17:55.450893Z","iopub.status.idle":"2023-06-06T19:18:16.080576Z","shell.execute_reply.started":"2023-06-06T19:17:55.450861Z","shell.execute_reply":"2023-06-06T19:18:16.079491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}