-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_one_hotdog.py
101 lines (76 loc) · 2.75 KB
/
preprocess_one_hotdog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.3'
# jupytext_version: 0.8.6
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# +
import os
import numpy as np
import tensorflow as tf
import keras
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.applications.inception_v3 import InceptionV3, preprocess_input
keras.backend.clear_session()
# +
##define paths and constants
# Remove src from cwd if necessary
cwd = os.getcwd()
print(cwd)
if os.path.basename(cwd) == 'src': cwd = os.path.dirname(cwd)
# Create img directory to save images if needed
os.makedirs(os.path.join(cwd, 'demo/test'), exist_ok=True)
#define future datapath
single_img_path = os.path.join(cwd, 'demo')
batch_size = 32
TARGET_SIZE = 299
# -
def image_data_augment(rescale=1/255, shear_range = False, zoom_range = False, horizontal_flip = False):
#declare ImageDataGenerator class for augmenting images using shear, zoom, and flips
#normalize with 1./255
return (ImageDataGenerator(
rescale=rescale,
shear_range=shear_range,
zoom_range=zoom_range,
horizontal_flip=horizontal_flip))
# +
def get_image():
datagen = image_data_augment()
generator = datagen.flow_from_directory(
single_img_path,
target_size=(TARGET_SIZE, TARGET_SIZE),
#batch_size=batch_size,
class_mode=None,
shuffle=False)
return generator
def preprocess_inception():
"""
inception for transfer learning
"""
#transfer learning with InceptionV3, a pre-trained cnn
model = InceptionV3(weights='imagenet', include_top=False, input_shape=(TARGET_SIZE, TARGET_SIZE, 3))
generator = get_image()
bottleneck_features = model.predict(generator, len(generator), verbose=1)
#save model with the bottleneck features
np.savez(f'inception_features_test_image', features=bottleneck_features)
return True
def get_single_image_data():
#augment images and use inception net
preprocess_inception()
#load training data and define labels, where 0 is hotdog and 1 is nothotdog
test_image_data = np.load('inception_features_test_image.npz')['features']
return test_image_data
# # load the output of inceptionv3 for our test image
# test_image_data = get_single_image_data()
# -
# **Authors**: Victoria Liu and Gloria Liu
# **Last modified**: November 2020
# Description: A script to pre-process a single image that is dropped into the image drop of the Flask app
# **Credits** The data augmentation code is heavily modified from [J-Yash's open-source code](https://github.com/J-Yash/Hotdog-Not-Hotdog).