Software engineering
Download 1.58 Mb.
|
tasviramaliy4
- Bu sahifa navigatsiya:
- 2 unit softmax dense layer
- MyLabelBinarizer()
- ImageDataGenerator
https://medium.com/@1297rohit/transfer-learning-from-scratch-using-keras-339834b153b9
for layers in (vggmodel.layers)[:15]: print(layers) layers.trainable = FalseX= vggmodel.layers[-2].output predictions = Dense(2, activation="softmax")(X) model_final = Model(input = vggmodel.input, output = predictions) opt = Adam(lr=0.0001) model_final.compile(loss = keras.losses.categorical_crossentropy, optimizer = opt, metrics=["accuracy"]) model_final.summary() In this part in the loop we are freezing the first 15 layers of the model. After that we are taking out the second last layer of the model and then adding a 2 unit softmax dense layer as we have just 2 classes to predict i.e. foreground or background. After that we are compiling the model using Adam optimizer with learning rate of 0.001. We are using categorical_crossentropy as loss since the output of the model is categorical. Finally the summary of the model will is printed using model_final.summary(). The image of summary is attached below. Model Summary from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelBinarizerclass MyLabelBinarizer(LabelBinarizer): def transform(self, y): Y = super().transform(y) if self.y_type_ == 'binary': return np.hstack((Y, 1-Y)) else: return Y def inverse_transform(self, Y, threshold=None): if self.y_type_ == 'binary': return super().inverse_transform(Y[:, 0], threshold) else: return super().inverse_transform(Y, threshold)lenc = MyLabelBinarizer() Y = lenc.fit_transform(y_new)X_train, X_test , y_train, y_test = train_test_split(X_new,Y,test_size=0.10) After creating the model now we need to split the dataset into train and test set. Before that we need to one-hot encode the label. For that we are using MyLabelBinarizer() and encoding the dataset. Then we are splitting the dataset using train_test_split from sklearn. We are keeping 10% of the dataset as test set and 90% as training set. trdata = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=90) traindata = trdata.flow(x=X_train, y=y_train) tsdata = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=90) testdata = tsdata.flow(x=X_test, y=y_test) Now we will use Keras ImageDataGenerator to pass the dataset to the model. We will do some augmentation on the dataset like horizontal flip, vertical flip and rotation to increase the dataset. from keras.callbacks import ModelCheckpoint, EarlyStoppingcheckpoint = ModelCheckpoint("ieeercnn_vgg16_1.h5", monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)early = EarlyStopping(monitor='val_loss', min_delta=0, patience=100, verbose=1, mode='auto')hist = model_final.fit_generator(generator= traindata, steps_per_epoch= 10, epochs= 1000, validation_data= testdata, validation_steps=2, callbacks=[checkpoint,early]) Now we start the training of the model using fit_generator. z=0 for e,i in enumerate(os.listdir(path)): if i.startswith("4"): z += 1 img = cv2.imread(os.path.join(path,i)) ss.setBaseImage(img) ss.switchToSelectiveSearchFast() ssresults = ss.process() imout = img.copy() for e,result in enumerate(ssresults): if e < 2000: x,y,w,h = result timage = imout[y:y+h,x:x+w] resized = cv2.resize(timage, (224,224), interpolation = cv2.INTER_AREA) img = np.expand_dims(resized, axis=0) out= model_final.predict(img) if out[0][0] > 0.70: cv2.rectangle(imout, (x, y), (x+w, y+h), (0, 255, 0), 1, cv2.LINE_AA) plt.figure() plt.imshow(imout) break Now once we have created the model. We need to do prediction on that model. For that we need to follow the steps mentioned below : pass the image from selective search. pass all the result of the selective search to the model as input using model_final.predict(img). If the output of the model says the region to be a foreground image (i.e. airplane image) and if the confidence is above the defined threshold then create bounding box on the original image on the coordinate of the proposed region. Output of the model As you can see above we created box on the proposed region in which the accuracy of the model was above 0.70. In this way we can do localisation on an image and perform object detection using R-CNN. This is how we implement an R-CNN architecture from scratch using keras. You can get the fully implemented R-CNN from the link provided below. ConclusionComputer vision conferences have been viewing new radical concepts each year and step by step I guess we are moving towards jaw-dropping performances from AI(if not already!). It only gets better. I hope the concepts were made lucid in this article, thank you :) Download 1.58 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling