priyankasharma5882 commited on
Commit
8ed216a
1 Parent(s): 1feee14

Upload breed-classification.ipynb

Browse files
Files changed (1) hide show
  1. breed-classification.ipynb +1 -0
breed-classification.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"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":"# Let's import the necessary packages \nimport os \nimport numpy \n\nimport cv2 # preprocessing I need this \nimport warnings \nimport numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt \nwarnings.filterwarnings('ignore')\n\nimport tensorflow as tf \nfrom keras.models import Sequential \nfrom tensorflow.keras.layers import Dense,Flatten,Conv2D,MaxPooling2D, GlobalAveragePooling2D\nfrom keras.preprocessing.image import ImageDataGenerator, load_img \n\nfrom IPython import display \ndisplay.set_matplotlib_formats('svg')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-20T07:08:54.419848Z","iopub.execute_input":"2022-05-20T07:08:54.420244Z","iopub.status.idle":"2022-05-20T07:09:00.299011Z","shell.execute_reply.started":"2022-05-20T07:08:54.420131Z","shell.execute_reply":"2022-05-20T07:09:00.298201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data loading \ntrain_gen = ImageDataGenerator(rescale = 1./255) # 0 to 255 pixels \n\ntrain_data = train_gen.flow_from_directory('../input/breedclassification/Breed Classification', target_size = (224, 224), \n batch_size = 32, class_mode = 'categorical', shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T07:09:05.208729Z","iopub.execute_input":"2022-05-20T07:09:05.209183Z","iopub.status.idle":"2022-05-20T07:09:05.32511Z","shell.execute_reply.started":"2022-05-20T07:09:05.209148Z","shell.execute_reply":"2022-05-20T07:09:05.324383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transfer learning \nfrom keras.applications.mobilenet_v2 import MobileNetV2,preprocess_input \n\nmodel = Sequential()\nbase_model = MobileNetV2(input_shape = [224, 224, 3], weights = 'imagenet', include_top = False) ","metadata":{"execution":{"iopub.status.busy":"2022-05-20T07:17:34.99469Z","iopub.execute_input":"2022-05-20T07:17:34.994957Z","iopub.status.idle":"2022-05-20T07:17:38.910577Z","shell.execute_reply.started":"2022-05-20T07:17:34.994929Z","shell.execute_reply":"2022-05-20T07:17:38.909782Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model correction \nbase_model.trainable = True \n\nfor i in base_model.layers[:100]: \n base_model.trainable = False\n \npool = GlobalAveragePooling2D()\nmid_layer = Dense(100,activation = 'relu')\nfinal_1 = Dense(7,activation = 'softmax')\n\nmodel = Sequential([base_model, mid_layer,pool, final_1])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-20T07:20:46.220311Z","iopub.execute_input":"2022-05-20T07:20:46.220888Z","iopub.status.idle":"2022-05-20T07:20:47.030742Z","shell.execute_reply.started":"2022-05-20T07:20:46.220854Z","shell.execute_reply":"2022-05-20T07:20:47.030037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = 'categorical_crossentropy',optimizer = 'adam',metrics = 'accuracy')\nmodel.fit(train_data, epochs = 100) # one variable (history)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T07:30:26.811674Z","iopub.execute_input":"2022-05-20T07:30:26.811964Z","iopub.status.idle":"2022-05-20T08:11:30.922475Z","shell.execute_reply.started":"2022-05-20T07:30:26.81193Z","shell.execute_reply":"2022-05-20T08:11:30.921801Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the accuracy! \n# plt.plot(history.history['acc'])\n# plt.title('model accuracy')\n# plt.ylabel('accuracy')\n# plt.xlabel('epoch')\n# plt.legend('train', loc='upper left')\n# plt.show()\n\n# # Plot the loss! \n# plt.plot(model.history['loss'])\n# plt.title('model loss')\n# plt.ylabel('loss')\n# plt.xlabel('epoch')\n# plt.legend('Loss', loc='upper left')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:12:21.391741Z","iopub.execute_input":"2022-05-20T08:12:21.392578Z","iopub.status.idle":"2022-05-20T08:12:21.397194Z","shell.execute_reply.started":"2022-05-20T08:12:21.39253Z","shell.execute_reply":"2022-05-20T08:12:21.396297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the new thing \ndef predict_classes(link): \n img = cv2.imread(link)\n img = cv2.resize(img,(224,224))\n img = img/255\n img = img.reshape(-1,224,224,3) # -1 is for reshaping basically \n pred = np.round(model.predict(img)).argmax(axis = 1)\n dic = {0: 'Herding breed', 1: 'Hound breed', 2: 'Non sporting breed', 3: 'Terrior breed', 4:'working breed', 5: 'sporting breed', 6: 'toy breed'}\n print(dic.get(int(pred)))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:12:24.374276Z","iopub.execute_input":"2022-05-20T08:12:24.375045Z","iopub.status.idle":"2022-05-20T08:12:24.380949Z","shell.execute_reply.started":"2022-05-20T08:12:24.374999Z","shell.execute_reply":"2022-05-20T08:12:24.380296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict \nlink = './sample_image.png'\npredict_classes(link)\nprint()\nload_img(link)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:35:03.426257Z","iopub.execute_input":"2022-05-20T08:35:03.426523Z","iopub.status.idle":"2022-05-20T08:35:07.008956Z","shell.execute_reply.started":"2022-05-20T08:35:03.426494Z","shell.execute_reply":"2022-05-20T08:35:07.007834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classification report \ntrue_classes = train_data.classes\nclass_labels = list(train_data.class_indices.keys())","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:14:55.038042Z","iopub.execute_input":"2022-05-20T08:14:55.038798Z","iopub.status.idle":"2022-05-20T08:14:55.043095Z","shell.execute_reply.started":"2022-05-20T08:14:55.038756Z","shell.execute_reply":"2022-05-20T08:14:55.042264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report,plot_confusion_matrix\n\ny_hat = model.predict(train_data)\npredicted_classes = np.argmax(y_hat, axis = 1)\nreport = classification_report(true_classes, predicted_classes, target_names = class_labels)\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:15:02.860499Z","iopub.execute_input":"2022-05-20T08:15:02.860763Z","iopub.status.idle":"2022-05-20T08:15:27.820247Z","shell.execute_reply.started":"2022-05-20T08:15:02.860734Z","shell.execute_reply":"2022-05-20T08:15:27.819427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving the model \nmodel.save('breedclassification.h5')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:27:44.88216Z","iopub.execute_input":"2022-05-20T08:27:44.882983Z","iopub.status.idle":"2022-05-20T08:27:45.324031Z","shell.execute_reply.started":"2022-05-20T08:27:44.882876Z","shell.execute_reply":"2022-05-20T08:27:45.323219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to load the model \n# new_model = tf.keras.models.load_model('breedclassification.h5')\n# new_model.predict(test_data) ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import requests\nresponse = requests.get(\"https://www.groomers-online.com/images/cms/sections/1598536220-35522500.png\")\n\nfile = open(\"sample_image.png\", \"wb\")\nfile.write(response.content)\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:43:50.454966Z","iopub.execute_input":"2022-05-20T08:43:50.455586Z","iopub.status.idle":"2022-05-20T08:43:51.882885Z","shell.execute_reply.started":"2022-05-20T08:43:50.455548Z","shell.execute_reply":"2022-05-20T08:43:51.882115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict \nlink = './sample_image.png'\npredict_classes(link)\nprint()\nload_img(link)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:44:00.376021Z","iopub.execute_input":"2022-05-20T08:44:00.376748Z","iopub.status.idle":"2022-05-20T08:44:00.522812Z","shell.execute_reply.started":"2022-05-20T08:44:00.376709Z","shell.execute_reply":"2022-05-20T08:44:00.522111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to load the model \nnew_model = tf.keras.models.load_model('breedclassification.h5')\n","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:44:51.233015Z","iopub.execute_input":"2022-05-20T08:44:51.233722Z","iopub.status.idle":"2022-05-20T08:44:52.648856Z","shell.execute_reply.started":"2022-05-20T08:44:51.233684Z","shell.execute_reply":"2022-05-20T08:44:52.648108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the new thing \ndef predict_classes(link): \n img = cv2.imread(link)\n img = cv2.resize(img,(224,224))\n img = img/255\n img = img.reshape(-1,224,224,3) # -1 is for reshaping basically \n pred = np.round(new_model.predict(img)).argmax(axis = 1)\n dic = {0: 'Herding breed', 1: 'Hound breed', 2: 'Non sporting breed', 3: 'Terrior breed', 4:'working breed', 5: 'sporting breed', 6: 'toy breed'}\n print(dic.get(int(pred)))\n\n# predict \nlink = './sample_image.png'\npredict_classes(link)\nprint()\nload_img(link)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:45:29.228947Z","iopub.execute_input":"2022-05-20T08:45:29.229628Z","iopub.status.idle":"2022-05-20T08:45:30.263623Z","shell.execute_reply.started":"2022-05-20T08:45:29.229588Z","shell.execute_reply":"2022-05-20T08:45:30.262901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install gradio ","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:49:34.944264Z","iopub.execute_input":"2022-05-20T08:49:34.944615Z","iopub.status.idle":"2022-05-20T08:49:53.386843Z","shell.execute_reply.started":"2022-05-20T08:49:34.944576Z","shell.execute_reply":"2022-05-20T08:49:53.385866Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gradio as gr\nimage = gr.inputs.Image(shape=(224, 224))\nlabel = gr.outputs.Label(num_top_classes=7)\n\ngr.Interface(fn=predict_classes, inputs=image, outputs=label,interpretation='default').launch() ","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:50:55.632124Z","iopub.execute_input":"2022-05-20T08:50:55.632714Z","iopub.status.idle":"2022-05-20T08:50:59.347486Z","shell.execute_reply.started":"2022-05-20T08:50:55.632677Z","shell.execute_reply":"2022-05-20T08:50:59.346601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}