{"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\nimport numpy as np \nimport pandas as pd\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\n\nimport os\nimport cv2\n# for 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\n# Load the data\n# Load the data\ntrain_data = pd.read_csv('/kaggle/input/extracting-attributes-from-fashion-images-2/train.csv')\n\n# Prepare the data\nX = train_data['file_name']  # Image file names\ny = train_data['label']  # Class labels\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)\n# Define data augmentation and preprocessing\ntrain_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.efficientnet.preprocess_input,\n)\n# Split the data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)\n# Define data augmentation and preprocessing\ntrain_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.efficientnet.preprocess_input,\n)\nval_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\ntrain = pd.DataFrame()\ntrain['file_name'] = X_train \ntrain['label'] = y_train\n# Convert numeric labels to strings\ntrain['label'] = train['label'].astype(str)\n\n# Create the train generator\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=train,\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=(100, 100),\n    batch_size=32,\n    class_mode='sparse'\n)\nvalid = pd.DataFrame()\nvalid['file_name'] = X_val \nvalid['label'] = y_val\n# Convert numeric labels to strings\nvalid['label'] = valid['label'].astype(str)\nval_generator = val_datagen.flow_from_dataframe(\n    dataframe=valid,\n    x_col=\"file_name\",\n    y_col=\"label\",\n    directory='/kaggle/input/extracting-attributes-from-fashion-images-2/train',\n    #subset=\"validation\",  # Use the validation subset of the data\n    shuffle=True,\n    target_size=(100, 100),  # Update the target size if needed\n    batch_size=32,\n    class_mode='sparse'\n)\n# Define the CNN model architecture\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(100, 100, 3)),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(64, 3, activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(64, 3, activation='relu'),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dense(7, activation='softmax')\n])\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n# Train the model\nhistory = model.fit(\n    train_generator,\n    validation_data=val_generator,\n    epochs=20\n)\ntest = pd.read_csv('/kaggle/input/extracting-attributes-from-fashion-images-2/sample_submission.csv')\ntest_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.efficientnet.preprocess_input,\n)\ntest['label'] = test['label'].astype(str)\ntest_images = test_generator.flow_from_dataframe(\n    dataframe=test,\n    x_col='file_name',\n    y_col='label',\n    directory='/kaggle/input/extracting-attributes-from-fashion-images-2/test',\n    target_size=(100,100),\n    batch_size=32,\n    shuffle=False\n)\npred = model.predict(test_images)\npred = np.argmax(pred,axis=1)\nsub = pd.read_csv('/kaggle/input/extracting-attributes-from-fashion-images-2/sample_submission.csv')\nsub['label'] = pred\nsub.to_csv('final_result.csv')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-07T06:30:03.344908Z","iopub.execute_input":"2023-06-07T06:30:03.345294Z"},"trusted":true},"execution_count":null,"outputs":[]}]}