<jupyter_start><jupyter_text><jupyter_code>import keras
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
from keras.datasets import cifar10
(X_train,y_train),(X_test,y_test)=cifar10.load_data()
import keras
import tensorflow as tf
X_train.shape
X_train.shape
plt.figure(figsize=(12,7))
for i in range(5):
  plt.subplot(230+1+i)
  plt.imshow(X_train[i])
import keras
from keras.models import Sequential
from keras.layers import Conv2D,Dropout,Flatten,MaxPooling2D,Dense

X_train.shape
X_test.shape
X_train=X_train.astype('float32')
X_test=X_test.astype('float32')

X_train=X_train/255.0
X_test=X_test/255.0
X_train[0][0]
y_train.shape
X_test

model=Sequential()

model.add(Conv2D(filters=32,kernel_size=(3,3),strides=(1,1),padding='same',activation='relu',input_shape=(32,32,3)))
model.add(Dropout(0.2))
model.add(Conv2D(filters=32,kernel_size=(3,3),strides=(1,1),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(units=512,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10,activation='softmax'))

model.summary()
import tensorflow as tf

from tensorflow.keras.optimizers import SGD

sgd=SGD(learning_rate=0.01,momentum=0.9,nesterov=False)
model.compile(optimizer=sgd,loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.summary()
model.fit(X_train,y_train,validation_data=(X_test,y_test),batch_size=32,epochs=10)
model.evaluate(X_test,y_test)
model.save('/content/drive/MyDrive/DL Practice/image recognition/image_recognition_model.h5')
import numpy as np
from PIL import Image

results={
   0:'aeroplane',
   1:'automobile',
   2:'bird',
   3:'cat',
   4:'deer',
   5:'dog',
   6:'frog',
   7:'horse',
   8:'ship',
   9:'truck'
}
im=Image.open('/content/drive/MyDrive/DL Practice/image recognition/horse.jpg')
im=im.resize((32,32))
im=np.expand_dims(im,axis=0)
im-np.array(im)
pred=model.predict([im])
pred=pred.argmax()
pred
results[pred]<jupyter_output><empty_output><jupyter_text>Building GUI using tkinter<jupyter_code>import tkinter as tk
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image
import numpy

#load the trained model to classify the images

from keras.models import load_model
model = load_model('/content/drive/MyDrive/DL Practice/image recognition/image_recognition_model.h5')

#dictionary to label all the CIFAR-10 dataset classes.

classes = { 
    0:'aeroplane',
    1:'automobile',
    2:'bird',
    3:'cat',
    4:'deer',
    5:'dog',
    6:'frog',
    7:'horse',
    8:'ship',
    9:'truck' 
}
#initialise GUI

top=tk.Tk()
top.geometry('800x600')
top.title('Image Classification CIFAR10')
top.configure(background='#CDCDCD')
label=Label(top,background='#CDCDCD', font=('arial',15,'bold'))
sign_image = Label(top)

def classify(file_path):
    global label_packed
    image = Image.open(file_path)
    image = image.resize((32,32))
    image = numpy.expand_dims(image, axis=0)
    image = numpy.array(image)
    pred = model.predict_classes([image])[0]
    sign = classes[pred]
    print(sign)
    label.configure(foreground='#011638', text=sign) 

def show_classify_button(file_path):
    classify_b=Button(top,text="Classify Image",
   command=lambda: classify(file_path),padx=10,pady=5)
    classify_b.configure(background='#364156', foreground='white',
font=('arial',10,'bold'))
    classify_b.place(relx=0.79,rely=0.46)

def upload_image():
    try:
        file_path=filedialog.askopenfilename()
        uploaded=Image.open(file_path)
  uploaded.thumbnail(((top.winfo_width()/2.25),
    (top.winfo_height()/2.25)))
        im=ImageTk.PhotoImage(uploaded)
        sign_image.configure(image=im)
        sign_image.image=im
        label.configure(text='')
        show_classify_button(file_path)
    except:
        pass

upload=Button(top,text="Upload an image",command=upload_image,
  padx=10,pady=5)

upload.configure(background='#364156', foreground='white',
    font=('arial',10,'bold'))

upload.pack(side=BOTTOM,pady=50)
sign_image.pack(side=BOTTOM,expand=True)
label.pack(side=BOTTOM,expand=True)
heading = Label(top, text="Image Classification CIFAR10",pady=20, font=('arial',20,'bold'))

heading.configure(background='#CDCDCD',foreground='#364156')
heading.pack()
top.mainloop()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1554524361948956, 'fraction_numerical': 0.038747099767981436, 'mean_word_length': 6.306779661016949, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '410484', 'license': 'Apache-2.0', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1616, 'n_words': 240, 'path': 'image_recognition.ipynb', 'repo_name': 'Lomesh2000/Image-Recognition', 'size': '4310'}