{"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":"from keras.preprocessing.image import load_img,ImageDataGenerator\nfrom keras.applications.xception import Xception\nfrom glob import glob\nfrom keras.layers import Dense,Conv2D,Flatten\nfrom keras.models import Model\nfrom keras.callbacks import ReduceLROnPlateau,RemoteMonitor\n","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:22.007069Z","iopub.execute_input":"2021-08-12T04:48:22.007481Z","iopub.status.idle":"2021-08-12T04:48:27.206484Z","shell.execute_reply.started":"2021-08-12T04:48:22.007378Z","shell.execute_reply":"2021-08-12T04:48:27.205664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir= '../input/dogs-cats-images/dataset/training_set/'\ntest_dir= '../input/dogs-cats-images/dataset/test_set'\nIMAGE_SIZE= [224, 224]","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:27.207964Z","iopub.execute_input":"2021-08-12T04:48:27.208313Z","iopub.status.idle":"2021-08-12T04:48:27.213710Z","shell.execute_reply.started":"2021-08-12T04:48:27.208276Z","shell.execute_reply":"2021-08-12T04:48:27.212770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model= Xception(input_shape=IMAGE_SIZE+[3],weights='imagenet',include_top=False)\nfor layer in base_model.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:27.215677Z","iopub.execute_input":"2021-08-12T04:48:27.216169Z","iopub.status.idle":"2021-08-12T04:48:31.005422Z","shell.execute_reply.started":"2021-08-12T04:48:27.216126Z","shell.execute_reply":"2021-08-12T04:48:31.004399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folders=glob(train_dir+'/*')\nlen(folders)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:31.006920Z","iopub.execute_input":"2021-08-12T04:48:31.007230Z","iopub.status.idle":"2021-08-12T04:48:31.025111Z","shell.execute_reply.started":"2021-08-12T04:48:31.007196Z","shell.execute_reply":"2021-08-12T04:48:31.024178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= Flatten()(base_model.output)\nx=Dense(64,activation='relu')(x)\nx=Dense(32,activation='relu')(x)\nprediction=Dense(len(folders)-1,activation='softmax')(x)\nmodel=Model(inputs=base_model.input,outputs=prediction)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:31.026378Z","iopub.execute_input":"2021-08-12T04:48:31.026739Z","iopub.status.idle":"2021-08-12T04:48:31.064229Z","shell.execute_reply.started":"2021-08-12T04:48:31.026703Z","shell.execute_reply":"2021-08-12T04:48:31.063332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',optimizer='Ftrl',metrics=['accuracy','AUC'])\n","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:31.065359Z","iopub.execute_input":"2021-08-12T04:48:31.065698Z","iopub.status.idle":"2021-08-12T04:48:31.082292Z","shell.execute_reply.started":"2021-08-12T04:48:31.065650Z","shell.execute_reply":"2021-08-12T04:48:31.081516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(\nrescale=1./255,\nrotation_range=120,\nwidth_shift_range=0.2,\nshear_range=0.2,\nzoom_range=0.2,\nhorizontal_flip=True)\n\ntest_datagen=ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:31.083666Z","iopub.execute_input":"2021-08-12T04:48:31.084294Z","iopub.status.idle":"2021-08-12T04:48:31.089602Z","shell.execute_reply.started":"2021-08-12T04:48:31.084255Z","shell.execute_reply":"2021-08-12T04:48:31.088751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data= train_datagen.flow_from_directory(train_dir,target_size=(224, 224),batch_size=32,class_mode='binary')\ntest_data= test_datagen.flow_from_directory(test_dir,target_size=(224, 224),batch_size=32,class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:31.092199Z","iopub.execute_input":"2021-08-12T04:48:31.092721Z","iopub.status.idle":"2021-08-12T04:48:37.582131Z","shell.execute_reply.started":"2021-08-12T04:48:31.092677Z","shell.execute_reply":"2021-08-12T04:48:37.581322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:37.583599Z","iopub.execute_input":"2021-08-12T04:48:37.583961Z","iopub.status.idle":"2021-08-12T04:48:37.646890Z","shell.execute_reply.started":"2021-08-12T04:48:37.583921Z","shell.execute_reply":"2021-08-12T04:48:37.641662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,\n                              patience=5, min_lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:48:37.648127Z","iopub.execute_input":"2021-08-12T04:48:37.648454Z","iopub.status.idle":"2021-08-12T04:48:37.654312Z","shell.execute_reply.started":"2021-08-12T04:48:37.648419Z","shell.execute_reply":"2021-08-12T04:48:37.653529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit_generator(train_data,epochs=100,validation_data=test_data,callbacks=[reduce_lr],steps_per_epoch=50,validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T04:50:07.636916Z","iopub.execute_input":"2021-08-12T04:50:07.637244Z","iopub.status.idle":"2021-08-12T05:39:54.732805Z","shell.execute_reply.started":"2021-08-12T04:50:07.637214Z","shell.execute_reply":"2021-08-12T05:39:54.731912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# summarize history for accuracy\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T05:53:58.395675Z","iopub.execute_input":"2021-08-12T05:53:58.396075Z","iopub.status.idle":"2021-08-12T05:53:58.710607Z","shell.execute_reply.started":"2021-08-12T05:53:58.396044Z","shell.execute_reply":"2021-08-12T05:53:58.709507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}