-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,144 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import numpy as np | ||
|
||
from keras.models import Model | ||
from keras.layers import Dense, Dropout | ||
from keras.preprocessing.image import load_img, img_to_array | ||
import tensorflow as tf | ||
|
||
from nasnet import NASNetMobile, preprocess_input | ||
from utils import mean_score, std_score | ||
|
||
with tf.device('/CPU:0'): | ||
base_model = NASNetMobile((224, 224, 3), include_top=False, pooling='avg', weights=None) | ||
x = Dropout(0.75)(base_model.output) | ||
x = Dense(10, activation='softmax')(x) | ||
|
||
model = Model(base_model.input, x) | ||
model.load_weights('weights/nasnet_weights.h5', by_name=True) | ||
|
||
img_path = 'images/art1.jpg' | ||
img = load_img(img_path, target_size=(224, 224)) | ||
x = img_to_array(img) | ||
x = np.expand_dims(x, axis=0) | ||
|
||
x = preprocess_input(x) | ||
|
||
scores = model.predict(x, batch_size=1, verbose=1)[0] | ||
|
||
mean = mean_score(scores) | ||
std = std_score(scores) | ||
|
||
print("Evaluating : ", img_path) | ||
print("NIMA Score : %0.3f +- (%0.3f)" % (mean, std)) | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import numpy as np | ||
|
||
import tensorflow as tf | ||
from keras import backend as K | ||
from nasnet import NASNetMobile | ||
|
||
from data_loader import train_generator, val_generator | ||
|
||
sess = tf.Session() | ||
K.set_session(sess) | ||
|
||
image_size = 224 | ||
|
||
def _float32_feature_list(floats): | ||
return tf.train.Feature(float_list=tf.train.FloatList(value=floats)) | ||
|
||
model = NASNetMobile((image_size, image_size, 3), include_top=False, pooling='avg') | ||
model.summary() | ||
|
||
# ''' TRAIN SET ''' | ||
nb_samples = 250000 * 2 | ||
batchsize = 200 | ||
|
||
with sess.as_default(): | ||
generator = train_generator(batchsize, shuffle=False) | ||
writer = tf.python_io.TFRecordWriter('weights/nasnet_train.tfrecord') | ||
|
||
count = 0 | ||
for _ in range(nb_samples // batchsize): | ||
x_batch, y_batch = next(generator) | ||
|
||
with sess.as_default(): | ||
x_batch = model.predict(x_batch, batchsize, verbose=1) | ||
|
||
for i, (x, y) in enumerate(zip(x_batch, y_batch)): | ||
examples = { | ||
'features': _float32_feature_list(x.flatten()), | ||
'scores': _float32_feature_list(y.flatten()), | ||
} | ||
features = tf.train.Features(feature=examples) | ||
example = tf.train.Example(features=features) | ||
writer.write(example.SerializeToString()) | ||
|
||
count += batchsize | ||
|
||
print("Finished %0.2f percentage storing dataset" % (count * 100 / float(nb_samples))) | ||
|
||
writer.close() | ||
|
||
''' TRAIN SET ''' | ||
nb_samples = 5000 | ||
batchsize = 200 | ||
|
||
with sess.as_default(): | ||
generator = val_generator(batchsize) | ||
writer = tf.python_io.TFRecordWriter('weights/nasnet_val.tfrecord') | ||
|
||
count = 0 | ||
for _ in range(nb_samples // batchsize): | ||
x_batch, y_batch = next(generator) | ||
|
||
with sess.as_default(): | ||
x_batch = model.predict(x_batch, batchsize, verbose=1) | ||
|
||
for i, (x, y) in enumerate(zip(x_batch, y_batch)): | ||
examples = { | ||
'features': _float32_feature_list(x.flatten()), | ||
'scores': _float32_feature_list(y.flatten()), | ||
} | ||
features = tf.train.Features(feature=examples) | ||
example = tf.train.Example(features=features) | ||
writer.write(example.SerializeToString()) | ||
|
||
count += batchsize | ||
|
||
print("Finished %0.2f percentage storing dataset" % (count * 100 / float(nb_samples))) | ||
|
||
writer.close() |
Oops, something went wrong.