Вы находитесь на странице: 1из 5

CLASSIFICATION OF ROOMS:

from google.colab import drive


drive.mount('/content/drive', force_remount=True)

Importing the dataset

import pandas as pd
data1 = pd.read_csv(r'/content/drive/My Drive/rooms.csv',encodin
g='latin-1', header=None)

Viewing first few records of the dataset

data1.head()

Viewing number of instances per class label

We can see that the data is balanced by the number of instances


is only 500.

#Number of instances per class label


print(data1[1].value_counts())

Applying one hot encoding to the class label

labels = pd.get_dummies(data1[1])

Splitting the dataset into Train and test with the test
size = 33%
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data1[0], la
bels, test_size=0.33, random_state=42)
from PIL import Image
import requests
import matplotlib.pyplot as plt
import cv2
import urllib
import numpy as np
from skimage import io
from google.colab.patches import cv2_imshow
from numpy import expand_dims
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
train_images = []
for url in X_train:
img = io.imread(url)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
img = cv2.resize(img,(256,256))
train_images.append(img)

test_images = []
for url in X_test:
img = io.imread(url)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
img = cv2.resize(img,(256,256))
test_images.append(img)

Converting the images into a Numpy array of rank 4

Xtrain = np.asarray(train_images)
Xtest =np.asarray(test_images)

del train_images
del test_images

Xtrain.shape

Building a simple CNN

from keras import backend as K


from keras.models import Sequential
from keras.layers import Input, Dropout, Flatten, Conv2D, MaxPoo
ling2D, Dense, Activation
from keras.optimizers import RMSprop
from keras.callbacks import ModelCheckpoint, Callback, EarlyStop
ping
from keras.optimizers import Adam
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Input, Dropout, Flatten, Conv2D, MaxPoo
ling2D, Dense, Activation
from keras.optimizers import RMSprop
from keras.callbacks import ModelCheckpoint, Callback, EarlyStop
ping
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator, array_
to_img, img_to_array, load_img

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(256, 256, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))


model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(16))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation("sigmoid"))

model.compile(loss="binary_crossentropy",
optimizer="rmsprop",
metrics=["accuracy"])
training_data_generator = ImageDataGenerator(
rescale=1./255,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True)

Data Augmentation using ImageDataGenerator


Since the number of instances is only 500, applying Data Augment
ation techniques will increase the dataset size and this improve
s model training accuracy

test_data_generator = ImageDataGenerator(rescale=1./255)

print(X_train.shape)

training_generator = training_data_generator.flow(Xtrain,
y_train,
batch_size=32)

test_generator = test_data_generator.flow(Xtest,
y_test,
batch_size=32)

Fitting the dataset to the model


history = model.fit_generator(
training_generator,
steps_per_epoch=32,
epochs=20,
validation_data=test_generator,
validation_steps=32)

model.save_weights('20_epochs.h5')

Model Training an Validation Summary

# list all data in history


print(history.history.keys())

# summarize history for accuracy


plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

Вам также может понравиться