Skip to content
Snippets Groups Projects
Commit 7279cc66 authored by Gokul's avatar Gokul
Browse files

Initial commit

parent 062813ff
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt;
# Importing sklearn libraries
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score
import hypopt
# Importing hypopt library for grid search
from hypopt import GridSearch
# Importing Keras libraries
from keras.utils import np_utils
from keras.models import Sequential
from keras.applications import VGG16
from keras.applications import imagenet_utils
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.layers import Dense, Conv2D, MaxPooling2D
from keras.layers import Dropout, Flatten, GlobalAveragePooling2D
import warnings
warnings.filterwarnings('ignore')
```
%% Output
Using TensorFlow backend.
%% Cell type:code id: tags:
``` python
def create_features(dataset, pre_model):
x_scratch = []
# loop over the images
for imagePath in dataset:
# load the input image and image is resized to 224x224 pixels
image = load_img(imagePath, target_size=(224, 224))
image = img_to_array(image)
# preprocess the image by (1) expanding the dimensions and
# (2) subtracting the mean RGB pixel intensity from the
# ImageNet dataset
image = np.expand_dims(image, axis=0)
image = imagenet_utils.preprocess_input(image)
# add the image to the batch
x_scratch.append(image)
x = np.vstack(x_scratch)
features = pre_model.predict(x, batch_size=32)
features_flatten = features.reshape((features.shape[0], 7 * 7 * 512))
return x, features, features_flatten
```
%% Cell type:code id: tags:
``` python
# load the VGG16 network
print("[INFO] loading network...")
# chop the top dense layers, include_top=False
model = VGG16(weights="imagenet", include_top=False)
model.summary()
```
%% Output
[INFO] loading network...
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, None, None, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, None, None, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, None, None, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, None, None, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, None, None, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, None, None, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, None, None, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, None, None, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, None, None, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, None, None, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, None, None, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, None, None, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, None, None, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, None, None, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, None, None, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________
%% Cell type:code id: tags:
``` python
import h5py
```
%% Cell type:code id: tags:
``` python
f = h5py.File('pgm_pro.h5', 'r')
f
```
%% Output
<HDF5 file "pgm_pro.h5" (mode r)>
%% Cell type:code id: tags:
``` python
list(f.keys())
```
%% Output
['x']
%% Cell type:code id: tags:
``` python
d=f['x']
d.shape
```
%% Output
(262144, 96, 96, 3)
%% Cell type:code id: tags:
``` python
x_scratch = []
for image in d:
image = np.expand_dims(image, axis=0)
image = imagenet_utils.preprocess_input(image)
# add the image to the batch
x_scratch.append(image)
x = np.vstack(x_scratch)
features = pre_model.predict(x, batch_size=32)
features_flatten = features.reshape((features.shape[0], 7 * 7 * 512))
```
%% Cell type:code id: tags:
``` python
print(x.shape,features.shape,features_flatten.shape)
```
%% Cell type:code id: tags:
``` python
```
This diff is collapsed.
image_03.png

439 KiB

image_04.png

438 KiB

image_05.png

443 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment