This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = keras.Sequential() | |
| # Adding pre-trained model layers | |
| model.add(model_vgg) | |
| # Flattening the image pixels | |
| model.add(Flatten()) | |
| # Adding 2 dense layers with 4096 neurons | |
| model.add(Dense(4096, activation='relu')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model_vgg.trainable = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The entire network is present under VGG16 feature extractor | |
| from keras.applications.vgg16 import VGG16 | |
| model_vgg = VGG16(weights='imagenet', include_top=False, input_shape= (224,224,3)) | |
| # include_top: Whether to include the 3 fully-connected layers at the top of the network. | |
| # weights: 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. | |
| # input_shape: optional shape tuple, only to be specified if include_top is False | |
| # (otherwise the input shape has to be (224, 224, 3) (with channels_last data format) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Predicting the loaded image | |
| output = model.predict(test_img) | |
| # Condition of checking the label of the loaded or tested image | |
| if output[0][0] > output[0][1]: | |
| print("Cat") | |
| else: | |
| print("Dog") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Converting image to array | |
| test_img = np.asarray(test_img) | |
| # Expanding the dimensions | |
| test_img = np.expand_dims(test_img, axis=0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load the testing image | |
| test_img = image.load_img(r"/content/cat.jpg", target_size=(224,224)) | |
| test_img |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Fitting the model | |
| result = model.fit_generator(steps_per_epoch=100, generator=training_data, verbose=1, validation_data=testing_data, epochs=50 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Compiling the model | |
| model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Getting the summary of the model | |
| model.summary() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Initializing a Sequential model | |
| model = Sequential() | |
| # Creating first block- (2 Convolution + 1 Max pool) | |
| model.add(Conv2D(filters= 64, kernel_size= (3,3), strides= (1,1), padding='same', input_shape= (224, 224, 3), activation= 'relu')) | |
| model.add(Conv2D(filters= 64, kernel_size= (3,3), strides= (1,1), padding='same', activation= 'relu')) | |
| model.add(MaxPool2D(pool_size= (2,2), strides=(2,2))) | |
| # Creating second block- (2 Convolution + 1 Max pool) | |
| model.add(Conv2D(filters= 128, kernel_size= (3,3), strides= (1,1), padding='same', activation= 'relu')) |
NewerOlder