{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow.keras as keras\nimport tensorflow as tf\nfrom keras.models import Sequential, Model\nfrom keras.layers import Conv2D, Conv2DTranspose, MaxPool2D,MaxPooling2D, Flatten, Dense, Input, Reshape, LeakyReLU, BatchNormalization, UpSampling2D, Dropout,Activation\nfrom keras.callbacks import LambdaCallback\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-01T13:04:17.277218Z","iopub.execute_input":"2024-06-01T13:04:17.277599Z","iopub.status.idle":"2024-06-01T13:04:23.706222Z","shell.execute_reply.started":"2024-06-01T13:04:17.277569Z","shell.execute_reply":"2024-06-01T13:04:23.705442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:04:23.707710Z","iopub.execute_input":"2024-06-01T13:04:23.708190Z","iopub.status.idle":"2024-06-01T13:04:37.702248Z","shell.execute_reply.started":"2024-06-01T13:04:23.708164Z","shell.execute_reply":"2024-06-01T13:04:37.701150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\n!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:04:15.587353Z","iopub.status.idle":"2024-06-01T13:04:15.587706Z","shell.execute_reply.started":"2024-06-01T13:04:15.587534Z","shell.execute_reply":"2024-06-01T13:04:15.587550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p ./train/cat/\n!mkdir -p ./train/dog/\n!mv ./train/cat.* ./train/cat/\n!mv ./train/dog.* ./train/dog/","metadata":{"execution":{"iopub.status.busy":"2024-05-31T10:35:33.495569Z","iopub.execute_input":"2024-05-31T10:35:33.496122Z","iopub.status.idle":"2024-05-31T10:35:35.744280Z","shell.execute_reply.started":"2024-05-31T10:35:33.496077Z","shell.execute_reply":"2024-05-31T10:35:35.742200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Autoencoder","metadata":{}},{"cell_type":"code","source":"BATCH_AE = 64\nEPOCHS_AE = 7\nLR = 0.01","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:04:37.703631Z","iopub.execute_input":"2024-06-01T13:04:37.703908Z","iopub.status.idle":"2024-06-01T13:04:37.709011Z","shell.execute_reply.started":"2024-06-01T13:04:37.703880Z","shell.execute_reply":"2024-06-01T13:04:37.707885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_path_train = \"/kaggle/working/train\"\n\ntrain_datagen = ImageDataGenerator(\n        rescale=1 / 255.0,\n        rotation_range=20,\n        zoom_range=0.05,\n        width_shift_range=0.05,\n        height_shift_range=0.05,\n        shear_range=0.05,\n        horizontal_flip=True,\n        fill_mode=\"nearest\",\n        validation_split=0.10)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:04:37.711032Z","iopub.execute_input":"2024-06-01T13:04:37.711296Z","iopub.status.idle":"2024-06-01T13:04:37.721430Z","shell.execute_reply.started":"2024-06-01T13:04:37.711274Z","shell.execute_reply":"2024-06-01T13:04:37.720551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n    directory=src_path_train,\n    target_size=(64, 64),\n    color_mode=\"rgb\",\n    batch_size=BATCH_AE,\n    subset='training',\n    shuffle=True,\n    seed=42,\n    class_mode='input'  # делает так, чтобы и x, и y были x\n)\nval_generator = train_datagen.flow_from_directory(\n    directory=src_path_train,\n    target_size=(64, 64),\n    color_mode=\"rgb\",\n    batch_size=BATCH_AE,\n    subset='validation',\n    shuffle=False,\n    seed=42,\n    class_mode='input'\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:04:37.722381Z","iopub.execute_input":"2024-06-01T13:04:37.722681Z","iopub.status.idle":"2024-06-01T13:04:38.530036Z","shell.execute_reply.started":"2024-06-01T13:04:37.722658Z","shell.execute_reply":"2024-06-01T13:04:38.529245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"physical_devices = tf.config.list_physical_devices('GPU')\nif len(physical_devices) > 0:\n    print(\"Using GPU\")\n    for gpu in physical_devices:\n        tf.config.experimental.set_memory_growth(gpu, True)\nelse:\n    print(\"Using CPU\")","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:04:38.531072Z","iopub.execute_input":"2024-06-01T13:04:38.531389Z","iopub.status.idle":"2024-06-01T13:04:38.773469Z","shell.execute_reply.started":"2024-06-01T13:04:38.531363Z","shell.execute_reply":"2024-06-01T13:04:38.772495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models, optimizers, callbacks\n\n# Define the residual block\ndef residual_block(x, filters):\n    shortcut = layers.Conv2D(filters, (1, 1), padding='same')(x)  # Adjusting the shortcut\n    x = layers.Conv2D(filters, (3, 3), padding='same', activation='relu')(x)\n    x = layers.BatchNormalization()(x)\n    x = layers.Conv2D(filters, (3, 3), padding='same')(x)\n    x = layers.BatchNormalization()(x)\n    x = layers.add([shortcut, x])\n    x = layers.ReLU()(x)\n    return x\n\n# Encoder\ninputs = tf.keras.Input(shape=(64, 64, 3))\n\n# Contracting path with residual blocks\nc1 = residual_block(inputs, 64)\np1 = layers.MaxPooling2D((2, 2))(c1)\n\nc2 = residual_block(p1, 128)\np2 = layers.MaxPooling2D((2, 2))(c2)\n\nc3 = residual_block(p2, 256)\np3 = layers.MaxPooling2D((2, 2))(c3)\n\nc4 = residual_block(p3, 512)\np4 = layers.MaxPooling2D((2, 2))(c4)\n\n# Bottleneck\nflattened = layers.Flatten()(p4)\nencoded = layers.Dense(1024, activation='relu')(flattened)  # Reduced size for practical purposes\n\n# Decoder\nreshaped = layers.Dense(p4.shape[1] * p4.shape[2] * p4.shape[3], activation='relu')(encoded)\nreshaped = layers.Reshape((p4.shape[1], p4.shape[2], p4.shape[3]))(reshaped)\n\n# Expansive path with residual blocks and skip connections\nu1 = layers.Conv2DTranspose(512, (3, 3), strides=(2, 2), padding='same', activation='relu')(reshaped)\nu1 = layers.concatenate([u1, c4])\nu1 = residual_block(u1, 512)\n\nu2 = layers.Conv2DTranspose(256, (3, 3), strides=(2, 2), padding='same', activation='relu')(u1)\nu2 = layers.concatenate([u2, c3])\nu2 = residual_block(u2, 256)\n\nu3 = layers.Conv2DTranspose(128, (3, 3), strides=(2, 2), padding='same', activation='relu')(u2)\nu3 = layers.concatenate([u3, c2])\nu3 = residual_block(u3, 128)\n\nu4 = layers.Conv2DTranspose(64, (3, 3), strides=(2, 2), padding='same', activation='relu')(u3)\nu4 = layers.concatenate([u4, c1])\nu4 = residual_block(u4, 64)\n\ndecoded = layers.Conv2D(3, (3, 3), padding='same', activation='sigmoid')(u4)\n\n# Autoencoder model\nautoencoder = models.Model(inputs, decoded)\n\n# Define the learning rate and optimizer\ninitial_learning_rate = 0.001\nlr_schedule = optimizers.schedules.ExponentialDecay(\n    initial_learning_rate,\n    decay_steps=10000,\n    decay_rate=0.99,\n    staircase=True\n)\noptimizer = optimizers.Adam(learning_rate=lr_schedule)\n\n# Compile the model\nautoencoder.compile(optimizer=optimizer, loss='mse')\n\n# Summary of the model\nautoencoder.summary()\n\n# Example training\n# autoencoder.fit(train_data, train_labels, epochs=100, validation_split=0.2, callbacks=[checkpoint_cb, early_stopping_cb])","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:46:33.391159Z","iopub.execute_input":"2024-06-01T13:46:33.392130Z","iopub.status.idle":"2024-06-01T13:46:33.882416Z","shell.execute_reply.started":"2024-06-01T13:46:33.392092Z","shell.execute_reply":"2024-06-01T13:46:33.881538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import LambdaCallback\nfrom livelossplot import PlotLossesKeras\n\nhistory = autoencoder.fit(\n    train_generator,\n    epochs=EPOCHS_AE,\n    validation_data=val_generator,\n    callbacks=[PlotLossesKeras()]\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:46:39.295280Z","iopub.execute_input":"2024-06-01T13:46:39.296131Z","iopub.status.idle":"2024-06-01T13:58:25.101461Z","shell.execute_reply.started":"2024-06-01T13:46:39.296101Z","shell.execute_reply":"2024-06-01T13:58:25.099559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = next(iter(val_generator))\nexample = test[0][3,:,:,:]","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:59:50.006262Z","iopub.execute_input":"2024-06-01T13:59:50.006769Z","iopub.status.idle":"2024-06-01T13:59:50.151883Z","shell.execute_reply.started":"2024-06-01T13:59:50.006735Z","shell.execute_reply":"2024-06-01T13:59:50.150725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(example)","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:59:50.988408Z","iopub.execute_input":"2024-06-01T13:59:50.988800Z","iopub.status.idle":"2024-06-01T13:59:51.185365Z","shell.execute_reply.started":"2024-06-01T13:59:50.988770Z","shell.execute_reply":"2024-06-01T13:59:51.184385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = autoencoder.predict(np.array([example]))\nplt.imshow(output[0,:,:,:])","metadata":{"execution":{"iopub.status.busy":"2024-06-01T13:59:52.934155Z","iopub.execute_input":"2024-06-01T13:59:52.934892Z","iopub.status.idle":"2024-06-01T13:59:53.267005Z","shell.execute_reply.started":"2024-06-01T13:59:52.934858Z","shell.execute_reply":"2024-06-01T13:59:53.266093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}