{"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 fastdownload import download_url\nfrom fastai.vision.all import *\nimport fastai","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:32:46.552234Z","iopub.execute_input":"2022-09-01T21:32:46.552711Z","iopub.status.idle":"2022-09-01T21:32:49.187540Z","shell.execute_reply.started":"2022-09-01T21:32:46.552618Z","shell.execute_reply":"2022-09-01T21:32:49.186585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall()\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall()","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:32:49.189696Z","iopub.execute_input":"2022-09-01T21:32:49.190266Z","iopub.status.idle":"2022-09-01T21:33:07.898645Z","shell.execute_reply.started":"2022-09-01T21:32:49.190226Z","shell.execute_reply":"2022-09-01T21:33:07.897683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"./trainingData\")\nos.mkdir(\"./trainingData/cat\")\nos.mkdir(\"./trainingData/dog\")","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:33:07.900108Z","iopub.execute_input":"2022-09-01T21:33:07.900453Z","iopub.status.idle":"2022-09-01T21:33:07.906211Z","shell.execute_reply.started":"2022-09-01T21:33:07.900413Z","shell.execute_reply":"2022-09-01T21:33:07.905180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listoftraining=os.listdir(\"./train\")\ncatlist=[x  for x in listoftraining if x.split('.')[0]==\"cat\" ]\ndoglist=[x  for x in listoftraining if x.split('.')[0]==\"dog\" ]\ncatlist[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:33:07.909046Z","iopub.execute_input":"2022-09-01T21:33:07.909716Z","iopub.status.idle":"2022-09-01T21:33:07.946994Z","shell.execute_reply.started":"2022-09-01T21:33:07.909675Z","shell.execute_reply":"2022-09-01T21:33:07.946136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nfor imageName in catlist:\n    img=cv2.imread(\"./train/\"+imageName)\n    cv2.imwrite(\"./trainingData/cat/\"+imageName,img)\nfor imageName in doglist:\n    img=cv2.imread(\"./train/\"+imageName)\n    cv2.imwrite(\"./trainingData/dog/\"+imageName,img)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:33:07.948220Z","iopub.execute_input":"2022-09-01T21:33:07.949288Z","iopub.status.idle":"2022-09-01T21:35:05.605995Z","shell.execute_reply.started":"2022-09-01T21:33:07.949252Z","shell.execute_reply":"2022-09-01T21:35:05.604651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_folder(\"./trainingData/\",blocks=(ImageBlock, CategoryBlock),\n                  get_items=get_image_files,\n                  splitter=RandomSplitter(0.2),\n                  get_y=parent_label,\n                  vocab=[\"cat\",\"dog\"],valid_pct=0.2,item_tfms=RandomResizedCrop(460),\n                  batch_tfms=[*aug_transforms(size=224, max_warp=0,do_flip = True,max_rotate = 10), Normalize.from_stats(*imagenet_stats)]\n                                        )\ndls.show_batch(max_n=9)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:36:43.633707Z","iopub.execute_input":"2022-09-01T21:36:43.634177Z","iopub.status.idle":"2022-09-01T21:36:46.319656Z","shell.execute_reply.started":"2022-09-01T21:36:43.634138Z","shell.execute_reply":"2022-09-01T21:36:46.318590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.vocab","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:35:36.483974Z","iopub.execute_input":"2022-09-01T21:35:36.484445Z","iopub.status.idle":"2022-09-01T21:35:36.497482Z","shell.execute_reply.started":"2022-09-01T21:35:36.484405Z","shell.execute_reply":"2022-09-01T21:35:36.496267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner=cnn_learner(dls,resnet34,metrics=accuracy,cbs=ShowGraphCallback(),pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T21:44:42.310471Z","iopub.execute_input":"2022-09-01T21:44:42.311049Z","iopub.status.idle":"2022-09-01T21:44:43.317499Z","shell.execute_reply.started":"2022-09-01T21:44:42.311007Z","shell.execute_reply":"2022-09-01T21:44:43.316327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner.fine_tune(4)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-01T22:07:38.920221Z","iopub.execute_input":"2022-09-01T22:07:38.920756Z","iopub.status.idle":"2022-09-01T22:32:09.035701Z","shell.execute_reply.started":"2022-09-01T22:07:38.920707Z","shell.execute_reply":"2022-09-01T22:32:09.034358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learner)","metadata":{"execution":{"iopub.status.busy":"2022-09-01T22:37:24.830261Z","iopub.execute_input":"2022-09-01T22:37:24.830786Z","iopub.status.idle":"2022-09-01T22:38:28.854412Z","shell.execute_reply.started":"2022-09-01T22:37:24.830746Z","shell.execute_reply":"2022-09-01T22:38:28.853174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2022-09-01T22:39:11.569729Z","iopub.execute_input":"2022-09-01T22:39:11.570398Z","iopub.status.idle":"2022-09-01T22:40:15.700944Z","shell.execute_reply.started":"2022-09-01T22:39:11.570345Z","shell.execute_reply":"2022-09-01T22:40:15.699602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner.save('./model')","metadata":{"execution":{"iopub.status.busy":"2022-09-01T22:33:51.326317Z","iopub.execute_input":"2022-09-01T22:33:51.326806Z","iopub.status.idle":"2022-09-01T22:33:52.298233Z","shell.execute_reply.started":"2022-09-01T22:33:51.326766Z","shell.execute_reply":"2022-09-01T22:33:52.297249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}