Skip to content

Commit

Permalink
add test_network jupyter notebokk to network testing
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketFlash committed Aug 16, 2019
1 parent b5677a1 commit b570421
Show file tree
Hide file tree
Showing 7 changed files with 4,741 additions and 37 deletions.
6 changes: 3 additions & 3 deletions configs/road_signs.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
input_shape : [48, 48, 3]
encodings_len: 1024
encodings_len: 4096
mode : 'triplet'
distance_type : 'l1'
backbone : 'resnet50'
backbone : 'simple2'
backbone_weights : 'imagenet'
optimizer : 'adam'
learning_rate : 0.0001
Expand All @@ -14,4 +14,4 @@ dataset_path : '/home/rauf/plates_competition/dataset/road_signs/road_signs_sepa
tensorboard_log_path : 'tf_log/'
weights_save_path : 'weights/'
plots_path : 'plots/'
encodings_path : 'encodings/'
encodings_path : 'encodings/'
34 changes: 29 additions & 5 deletions data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import cv2
import numpy as np
import random
from matplotlib import pyplot as plt


class SiameseImageLoader:
Expand Down Expand Up @@ -35,7 +36,6 @@ def _load_images_paths(self):
self.images_paths[d].append(root+'/'+f)
self.images_labels[d].append(root.split('/')[-1])


def _get_images_set(self, clsss, idxs, s='train', with_aug=True):

indxs = [self.indexes[s][cl][idx] for cl, idx in zip(clsss, idxs)]
Expand Down Expand Up @@ -94,8 +94,9 @@ def get_batch_pairs(self, batch_size, s='train'):
return pairs, targets

def get_batch_triplets(self, batch_size, s='train'):
triplets = [np.zeros((batch_size, self.input_shape[0], self.input_shape[1], 3)),
np.zeros((batch_size, self.input_shape[0], self.input_shape[1], 3)),
triplets = [np.zeros((batch_size, self.input_shape[0], self.input_shape[1], 3)),
np.zeros(
(batch_size, self.input_shape[0], self.input_shape[1], 3)),
np.zeros((batch_size, self.input_shape[0], self.input_shape[1], 3))]
targets = np.zeros((batch_size,))

Expand All @@ -120,7 +121,7 @@ def get_batch_triplets(self, batch_size, s='train'):
idx3 = random.randrange(0, another_class_n_elements)
imgs = self._get_images_set(
[selected_class, selected_class, another_class], [idx1, idx2, idx3], s=s, with_aug=with_aug)

triplets[0][count, :, :, :] = imgs[0]
triplets[1][count, :, :, :] = imgs[1]
triplets[2][count, :, :, :] = imgs[2]
Expand All @@ -129,7 +130,7 @@ def get_batch_triplets(self, batch_size, s='train'):

return triplets, targets

def generate(self, batch_size, mode='siamese', s="train"):
def generate(self, batch_size, mode='siamese', s='train'):
while True:
if mode == 'siamese':
data, targets = self.get_batch_pairs(batch_size, s)
Expand All @@ -143,3 +144,26 @@ def get_image(self, img_path):
img = cv2.resize(
img, (self.input_shape[0], self.input_shape[1]))
return img

def plot_batch(self, batch_size, mode='triplets', s='train'):
if mode == 'triplets':
data, targets = self.get_batch_triplets(batch_size, s)
else:
data, targets = self.get_batch_pairs(batch_size, s)
num_imgs = data[0].shape[0]
it_val = 3 if mode == 'triplets' else 2
fig, axs = plt.subplots(num_imgs, it_val, figsize=(
30, 50), facecolor='w', edgecolor='k')
fig.subplots_adjust(hspace=.5, wspace=.001)

axs = axs.ravel()
i = 0
for img_idx, targ in zip(range(num_imgs), targets):
for j in range(it_val):
img = cv2.cvtColor(data[j][img_idx].astype(
np.uint8), cv2.COLOR_BGR2RGB)
axs[i+j].imshow(img)
axs[i+j].set_title(targ)
i += it_val

plt.show()
2 changes: 1 addition & 1 deletion model.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def generate_encodings(self, save_file_name='encodings.pkl', max_num_samples_of_
f.close()

def load_encodings(self, path_to_encodings):
utils.load_encodings(path_to_encodings)
self.encoded_training_data = utils.load_encodings(path_to_encodings)

def load_model(self,file_path):
self.model = load_model(file_path,
Expand Down
Empty file added test.py
Empty file.
4,645 changes: 4,645 additions & 0 deletions test_network.ipynb

Large diffs are not rendered by default.

11 changes: 3 additions & 8 deletions test_net.py → train.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,16 @@
decay_factor ** np.floor(x/step_size)),
EarlyStopping(patience=50, verbose=1),
TensorBoard(log_dir=model.tensorboard_log_path),
# ReduceLROnPlateau(factor=0.9, patience=50,
# min_lr=1e-12, verbose=1),
ModelCheckpoint(filepath=os.path.join(model.weights_save_path, 'best_model_0.h5'),
ModelCheckpoint(filepath=os.path.join(model.weights_save_path, 'best_model.h5'),
verbose=1, monitor='val_loss', save_best_only=True)
]

H = model.train_generator(steps_per_epoch=n_steps_per_epoch, callbacks=callbacks,
val_steps=val_steps, epochs=n_epochs)
model.train_generator(steps_per_epoch=n_steps_per_epoch, callbacks=callbacks,
val_steps=val_steps, epochs=n_epochs)


model.generate_encodings()
# model.load_encodings('encodings/encodings.pkl')
prediction = model.predict(
'/home/rauf/plates_competition/dataset/road_signs/road_signs_separated/val/7_1/rtsd-r3_test_009188.png')
print(prediction)

model_accuracy = model.calculate_prediction_accuracy()
print('Model accuracy on validation set: {}'.format(model_accuracy))
80 changes: 60 additions & 20 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,76 @@
from augmentations import get_aug
from data_loader import SiameseImageLoader


def load_encodings(path_to_encodings):

with open(path_to_encodings, 'rb') as f:
encodings = pickle.load(f)
return encodings


def make_tsne(project_name, show=True):
encodings = load_encodings(
'encodings/{}encodings.pkl'.format(project_name))
def plot_tsne(encodings_path, save_plot_dir, show=True):
encodings = load_encodings(encodings_path)
labels = list(set(encodings['labels']))
tsne = TSNE()
tsne_train = tsne.fit_transform(encodings['encodings'])
fig, ax = plt.subplots(figsize=(16, 16))
for i, l in enumerate(labels):
ax.scatter(tsne_train[np.array(encodings['labels']) == l, 0],
tsne_train[np.array(encodings['labels']) == l, 1], label=l)
ax.legend()
xs = tsne_train[np.array(encodings['labels']) == l, 0]
ys = tsne_train[np.array(encodings['labels']) == l, 1]
ax.scatter(xs, ys, label=l)
for x, y in zip(xs, ys):
plt.annotate(l,
(x, y),
size=8,
textcoords="offset points",
xytext=(0, 10),
ha='center')

ax.legend(bbox_to_anchor=(1.05, 1), fontsize='small', ncol=2)
if show:
fig.show()

fig.savefig("plots/{}{}.png".format(project_name, 'tsne.png'))
fig.savefig("{}{}.png".format(save_plot_dir, 'tsne.png'))


def plot_tsne_interactive(encodings_path):
import plotly.graph_objects as go
encodings = load_encodings(encodings_path)
labels = list(set(encodings['labels']))
tsne = TSNE()
tsne_train = tsne.fit_transform(encodings['encodings'])
fig = go.Figure()
for i, l in enumerate(labels):
xs = tsne_train[np.array(encodings['labels']) == l, 0]
ys = tsne_train[np.array(encodings['labels']) == l, 1]
color = 'rgba({},{},{},{})'.format(int(255*np.random.rand()),
int(255*np.random.rand()),
int(255*np.random.rand()), 0.8)
fig.add_trace(go.Scatter(x=xs,
y=ys,
mode='markers',
marker=dict(color=color,
size=10),
text=l,
name=l))
fig.update_layout(
title=go.layout.Title(text="t-SNE plot",
xref="paper",
x=0),
autosize=False,
width=1000,
height=1000
)

fig.show()


def parse_net_params(filename='configs/road_signs.yml'):
params = {}
with open(filename, 'r') as ymlfile:
cfg = yaml.load(ymlfile)
cfg = yaml.safe_load(ymlfile)

if cfg['learning_rate']:
learning_rate = cfg['learning_rate']
else:
Expand All @@ -53,22 +94,21 @@ def parse_net_params(filename='configs/road_signs.yml'):
augmentations = get_aug(cfg['augmentation_type'], cfg['input_shape'])
else:
augmentations = None


params = {k:v for k,v in cfg.items() if k not in ['optimizer']}
params['encodings_path'] = os.path.join(cfg['encodings_path'],
params = {k: v for k, v in cfg.items() if k not in ['optimizer']}
params['encodings_path'] = os.path.join(cfg['encodings_path'],
cfg['project_name'])
params['plots_path'] = os.path.join(cfg['plots_path'],
params['plots_path'] = os.path.join(cfg['plots_path'],
cfg['project_name'])
params['tensorboard_log_path'] = os.path.join(cfg['tensorboard_log_path'],
params['tensorboard_log_path'] = os.path.join(cfg['tensorboard_log_path'],
cfg['project_name'])
params['weights_save_path'] = os.path.join(cfg['weights_save_path'],
params['weights_save_path'] = os.path.join(cfg['weights_save_path'],
cfg['project_name'])

if 'dataset_path' in cfg:
params['loader'] = SiameseImageLoader(cfg['dataset_path'],
input_shape=cfg['input_shape'],
augmentations=augmentations)
params['loader'] = SiameseImageLoader(cfg['dataset_path'],
input_shape=cfg['input_shape'],
augmentations=augmentations)

params['optimizer'] = optimizer
return params
return params

0 comments on commit b570421

Please sign in to comment.