{"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 os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tqdm import tqdm\n\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-06T19:14:33.353810Z","iopub.execute_input":"2023-06-06T19:14:33.354879Z","iopub.status.idle":"2023-06-06T19:14:33.361516Z","shell.execute_reply.started":"2023-06-06T19:14:33.354837Z","shell.execute_reply":"2023-06-06T19:14:33.360231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the paths and filenames\ntrain_path = '/kaggle/input/extracting-attributes-from-fashion-images-2/train'\ntrain_csv = '/kaggle/input/extracting-attributes-from-fashion-images-2/train.csv'\ntest_path = '/kaggle/input/extracting-attributes-from-fashion-images-2/test'\noutput_csv = '/kaggle/working/OUTPUT_1.csv'","metadata":{"execution":{"iopub.status.busy":"2023-06-06T19:14:34.429635Z","iopub.execute_input":"2023-06-06T19:14:34.430154Z","iopub.status.idle":"2023-06-06T19:14:34.435807Z","shell.execute_reply.started":"2023-06-06T19:14:34.430103Z","shell.execute_reply":"2023-06-06T19:14:34.434686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(train_csv)\ntrain_images = []\ntrain_labels = []\nimage_format = 'Image_train_{:05d}.jpg'","metadata":{"execution":{"iopub.status.busy":"2023-06-06T19:34:21.922803Z","iopub.execute_input":"2023-06-06T19:34:21.923932Z","iopub.status.idle":"2023-06-06T19:34:21.943685Z","shell.execute_reply.started":"2023-06-06T19:34:21.923891Z","shell.execute_reply":"2023-06-06T19:34:21.942513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(train_data.shape[0]):\n    image_path = os.path.join(train_path, image_format.format(i + 1))\n    img_arr = cv2.imread(image_path)\n    if img_arr is None:\n        # Skip If Image not Found\n        continue\n    img_arr = cv2.resize(img_arr, (224, 224))\n    img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2GRAY)\n    img_arr = img_arr / 255.0\n    train_images.append(img_arr)\n    train_labels.append(train_data['label'][i])","metadata":{"execution":{"iopub.status.busy":"2023-06-06T19:34:22.505542Z","iopub.execute_input":"2023-06-06T19:34:22.505887Z","iopub.status.idle":"2023-06-06T19:36:05.466172Z","shell.execute_reply.started":"2023-06-06T19:34:22.505859Z","shell.execute_reply":"2023-06-06T19:36:05.464633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert train_images and train_labels to numpy arrays\ntrain_images = np.array(train_images)\ntrain_labels = np.array(train_labels)\n\n# Reshape train_images to have a single channel\ntrain_images = train_images.reshape(-1, 224, 224, 1)\n\n# Convert train_labels to one-hot encoded format\nunique_labels = np.unique(train_labels)\nnum_classes = len(unique_labels)\nlabel_to_index = {label: index for index, label in enumerate(unique_labels)}\ntrain_labels = np.array([label_to_index[label] for label in train_labels])\ntrain_labels = tf.keras.utils.to_categorical(train_labels, num_classes=num_classes)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-06T19:36:05.468081Z","iopub.execute_input":"2023-06-06T19:36:05.468418Z","iopub.status.idle":"2023-06-06T19:36:07.838295Z","shell.execute_reply.started":"2023-06-06T19:36:05.468390Z","shell.execute_reply":"2023-06-06T19:36:07.837052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 1)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-06T19:36:07.839551Z","iopub.execute_input":"2023-06-06T19:36:07.839867Z","iopub.status.idle":"2023-06-06T19:36:07.920356Z","shell.execute_reply.started":"2023-06-06T19:36:07.839842Z","shell.execute_reply":"2023-06-06T19:36:07.919266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss=tf.keras.losses.CategoricalCrossentropy(), metrics=['accuracy'])\nmodel.fit(train_images, train_labels, epochs=25)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T19:37:01.149134Z","iopub.execute_input":"2023-06-06T19:37:01.150349Z","iopub.status.idle":"2023-06-06T21:04:29.363958Z","shell.execute_reply.started":"2023-06-06T19:37:01.150306Z","shell.execute_reply":"2023-06-06T21:04:29.362619Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = []\nimage_files = os.listdir(test_path)  # Get the list of files in the test directory\nimage_files.sort()  # Sort the files in ascending order\n\noutput_data = {'file_name': [], 'label': []}","metadata":{"execution":{"iopub.status.busy":"2023-06-06T21:05:44.449418Z","iopub.execute_input":"2023-06-06T21:05:44.450349Z","iopub.status.idle":"2023-06-06T21:05:44.472727Z","shell.execute_reply.started":"2023-06-06T21:05:44.450314Z","shell.execute_reply":"2023-06-06T21:05:44.471619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, image_file in enumerate(image_files, start=1):\n    image_path = os.path.join(test_path, image_file)\n    img_arr = cv2.imread(image_path)\n    if img_arr is None:\n        #Skip None Image \n        continue\n    img_arr = cv2.resize(img_arr, (224, 224))\n    img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2GRAY)\n    img_arr = img_arr / 255.0\n    test_images.append(img_arr)\n\n    # Predict the class for the current test image\n    img_tensor = np.expand_dims(img_arr, axis=0)\n    prediction = model.predict(img_tensor)\n    predicted_class = np.argmax(prediction)\n    \n    output_data['file_name'].append(image_file)\n    output_data['label'].append(predicted_class)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T21:05:49.590107Z","iopub.execute_input":"2023-06-06T21:05:49.590953Z","iopub.status.idle":"2023-06-06T21:14:04.805757Z","shell.execute_reply.started":"2023-06-06T21:05:49.590923Z","shell.execute_reply":"2023-06-06T21:14:04.804477Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the predicted classes and file names in a CSV file\noutput_df = pd.DataFrame(output_data)\noutput_df.to_csv(output_csv, index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T21:15:28.557094Z","iopub.execute_input":"2023-06-06T21:15:28.557610Z","iopub.status.idle":"2023-06-06T21:15:28.593601Z","shell.execute_reply.started":"2023-06-06T21:15:28.557575Z","shell.execute_reply":"2023-06-06T21:15:28.592261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}