tidy up script usage

Esse commit está contido em:
Charlie Hewitt
2018-04-26 14:08:11 +01:00
commit 47befc2758
5 arquivos alterados com 98 adições e 141 exclusões
+7 -3
Ver Arquivo
@@ -1,5 +1,4 @@
import sys
import coremltools
@@ -32,11 +31,16 @@ def do(t, path):
def main(argv):
if len(argv) == 2:
if argv[0] == 'C' or argv[0] == 'R':
if argv[0] == 'c' or argv[0] == 'r':
do(argv[0], argv[1])
return
raise(Exception('INPUT ERROR'))
if __name__ == '__main__':
main(sys.argv[1:])
if len(sys.argv) != 3:
print('Usage:')
print('python convert.py -c path_to_keras_classifier_model')
print('python convert.py -r path_to_keras_regressor_model')
else:
main(sys.argv[1:])
+7 -15
Ver Arquivo
@@ -1,5 +1,3 @@
#! /usr/local/bin/python3
import csv
import os
import sys
@@ -252,20 +250,14 @@ if __name__ == '__main__':
if len(sys.argv) == 3:
if sys.argv[1] == '-c':
eval(c_path=sys.argv[2])
exit()
elif sys.argv[1] == '-r':
eval(r_path=sys.argv[2])
exit()
elif sys.argv[1] == '-f':
eval_from_file(sys.argv[2])
else:
raise Exception()
elif len(sys.argv) == 5:
c_path = None
r_path = None
if sys.argv[1] == '-c':
eval(c_path=sys.argv[2], r_path=sys.argv[4])
elif sys.argv[1] == '-r':
eval(r_path=sys.argv[2], c_path=sys.argv[4])
else:
raise Exception()
else:
raise Exception()
exit()
print('Usage:')
print('python evaluate.py -c path_to_classifier_keras_model')
print('python evaluate.py -r path_to_regressor_keras_model')
print('python evaluate.py -f path_to_user_study_data_file')
-46
Ver Arquivo
@@ -1,6 +1,5 @@
#! /usr/local/bin/python3
import csv
import os
import numpy as np
@@ -49,33 +48,6 @@ def load_images(C_or_R, paths, labels, batch_size=32, eval=False):
batch_n += 1
def load_paths(p):
labels = []
paths = []
count = 0
with open(p, 'r') as csvfile:
r = csv.reader(csvfile, delimiter=',')
count = 0
for row in r:
if 'sub' in row[0]:
continue
valence = float(row[-2])
arousal = float(row[-1])
emotion = int(row[-3])
path = 'data_p/' + row[0]
if emotion > 7 and (arousal == -2 or valence == -2):
continue
if not os.path.exists(path):
print('error: no image')
continue
labels.append((emotion, valence, arousal))
paths.append(path)
count += 1
print('Loaded:', count, end='\r')
print('Loaded:', count)
return paths, labels
def process_data(C_or_R, paths, labels):
labels_out = []
paths_out = []
@@ -101,19 +73,6 @@ def process_data(C_or_R, paths, labels):
labels_out = to_categorical(labels_out, num_classes=8)
else:
weights = None
# for i, (v, a) in enumerate(labels_out):
# if i % 300 == 0:
# m = np.mean(labels_out, axis=0)
# if abs(m[0]) < 0.05:
# break
# print(m[0], len(labels_out), end='\r')
# if (m[0] > 0 and v > 0) or (m[0] < 0 and v < 0):
# del labels_out[i]
# del paths_out[i]
# print(np.mean(labels_out, axis=0), np.std(labels_out, axis=0))
# print(len(labels_out))
# np.save('balanced_regr_labels', labels_out)
# np.save('balanced_regr_paths', paths_out)
print('Processed:', count)
return paths_out, labels_out, weights
@@ -369,11 +328,6 @@ def visualise(model, name):
def train(C_or_R, model, name, epochs, batch_size, test=False):
print('** LOADING DATA **')
# if C_or_R == REGRESS:
# t_paths = np.load('balanced_regr_paths.npy')
# t_labels = np.load('balanced_regr_labels.npy')
# print('Loaded', len(t_paths), 'balanced training data')
# else:
t_paths = np.load('training_paths.npy')
t_labels = np.load('training_labels.npy')
t_paths, t_labels, t_weights = process_data(C_or_R, t_paths, t_labels)
-77
Ver Arquivo
@@ -1,77 +0,0 @@
import csv
import os
import cv2
import numpy as np
def get_subimage(image, centre, theta, w, h):
if h > w:
theta += 90
t = h
h = w
w = t
rot = cv2.getRotationMatrix2D(centre, theta, 1)
rotated = cv2.warpAffine(image, rot, image.shape[:2])
out = cv2.getRectSubPix(rotated, (w, h), centre)
return out
def make_box(points, image, b=1):
rect = cv2.minAreaRect(np.array(points))
((x, y), (w, h), a) = rect
if w > h:
w, h = int(w * b), int(w * 0.5 * b)
else:
w, h = int(h * 0.5 * b), int(h * b)
rect = ((x, y), (w, h), a)
box = cv2.boxPoints(rect)
box = np.int0(box)
return get_subimage(image, (x, y), a, w, h)
def mouth_int(path, landmarks):
path = 'data/' + path
image = cv2.imread(path)
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return np.mean(make_box(landmarks[48:68], grey, b=2))
def load_data(p, limit=-1):
print('LOADING', p)
labelset = []
featureset = []
with open(p, 'r') as csvfile:
r = csv.reader(csvfile, delimiter=',')
count = 0
for row in r:
if 'sub' in row[0]:
continue
try:
valence = float(row[-2])
arousal = float(row[-1])
emotion = int(row[-3])
if (arousal == -2 or valence == -2) and emotion > 7:
continue
x, y, w, h = [float(row[1]), float(row[2]), float(row[3]), float(row[4])] # x, y, w, h
path = 'data/' + row[0]
o_path = 'data_p/' + row[0]
if not os.path.exists(o_path):
image = cv2.imread(path)[int(y):int(y + h), int(x):int(x + w)]
image = cv2.resize(image, (256, 256))
o_dir = '/'.join(o_path.split('/')[:-1])
if not os.path.exists(o_dir):
os.makedirs(o_dir)
cv2.imwrite(o_path, image)
count += 1
print('Processed:', count, end="\r")
if limit > 0 and count == limit: # TEMP
break
except Exception:
continue
print('\n')
return featureset, labelset
train_f, train_l = load_data('training.csv')
val_f, val_l = load_data('validation.csv')
+84
Ver Arquivo
@@ -0,0 +1,84 @@
import csv
import os
import cv2
import sys
import numpy as np
def process_images(p, limit=-1):
with open(p, 'r') as csvfile:
r = csv.reader(csvfile, delimiter=',')
count = 0
for row in r:
if 'sub' in row[0]:
continue
try:
valence = float(row[-2])
arousal = float(row[-1])
emotion = int(row[-3])
if (arousal == -2 or valence == -2) and emotion > 7:
continue
x, y, w, h = [float(row[1]), float(row[2]), float(row[3]), float(row[4])] # x, y, w, h
path = 'data/' + row[0]
o_path = 'data_p/' + row[0]
if not os.path.exists(o_path):
image = cv2.imread(path)[int(y):int(y + h), int(x):int(x + w)]
image = cv2.resize(image, (256, 256))
o_dir = '/'.join(o_path.split('/')[:-1])
if not os.path.exists(o_dir):
os.makedirs(o_dir)
cv2.imwrite(o_path, image)
count += 1
print('Processed:', count, end='\r')
if limit > 0 and count == limit: # TEMP
break
except Exception:
continue
print('Processed:', count)
def process_labels(p):
labels = []
paths = []
count = 0
with open(p, 'r') as csvfile:
r = csv.reader(csvfile, delimiter=',')
count = 0
for row in r:
if 'sub' in row[0]:
continue
valence = float(row[-2])
arousal = float(row[-1])
emotion = int(row[-3])
path = 'data_p/' + row[0]
if emotion > 7 and (arousal == -2 or valence == -2):
continue
if not os.path.exists(path):
print('error: no image')
continue
labels.append((emotion, valence, arousal))
paths.append(path)
count += 1
print('Loaded:', count, end='\r')
print('Loaded:', count)
return paths, labels
if __name__ == '__main__':
if '-i' not in sys.argv and '-l' not in sys.argv:
print('Usage:')
print('python process.py')
print(' -i process images from AffectNet training.csv + validation.csv')
print(' requires raw images in data dir and outputs to data_p')
print(' -l process labels from AffectNet training.csv + validation.csv')
print(' outputs to [training/validation]_[paths/labels].npy')
if '-i' in sys.argv:
process_images('training.csv')
process_images('validation.csv')
if '-l' in sys.argv:
tp, tl = process_labels('training.csv')
np.save(tp, 'training_paths')
np.save(tl, 'training_lables')
vp, vl = process_labels('validation.csv')
np.save(vp, 'validation_paths')
np.save(vl, 'validation_labels')