def get_image_data(): with open("./word.txt", "r") as f: words = f.read().split(",") words = [word.strip("\n") for word in words] images = [] labels = [] for index, word in enumerate(words): dir_path = "./images/{}".format(word) image_paths = os.listdir(dir_path) bar = pyprind.ProgBar(len(image_paths), track_time=True, title="Image {}".format(word)) for j, image_path in enumerate(image_paths): bar.update() if "jpg" not in image_path: continue image = np.asarray(Image.open(dir_path + "/" + image_path)) image_size = get_image_size() if image.shape != image_size: continue images.append(image) labels.append(index) images = np.asarray(images) labels = np.asarray(labels) return images, labels
def make_ensemble_predict_results(data_dir, target, batch_size, imglst, labels_txt, results_log, top_k, gpus): with open(imglst) as lst: test_list = [(l.split('\t')[1].strip(), l.split('\t')[2].strip()) for l in lst.readlines()] with open(labels_txt) as syn: labels = [l.split(' ')[-1].strip() for l in syn.readlines()] model_size_array = [] imgrec_array = [] for model_prefix in model_prefix_array: size = functions.get_image_size(model_prefix) model_size_array.append(size) imgrec_array.append("%s/images-%s-%d.rec" % (data_dir, target, size)) print(model_size_array) print(imgrec_array) with open(results_log, 'w') as result: result.write("model_prefix: %s\n" % ','.join(model_prefix_array)) result.write("model_epoch: %s\n" % ','.join([str(epoch) for epoch in model_epoch_array])) result.write("data: %s\n" % ','.join(imgrec_array)) pred_arrays = [] for model_prefix, model_epoch, model_size in zip( model_prefix_array, model_epoch_array, model_size_array): data_shape = (3, model_size, model_size) imgrec = "%s/images-%s-%d.rec" % (data_dir, target, model_size) mod = load_model(model_prefix, model_epoch, batch_size, model_size, gpus) test_rec = load_image_record(imgrec, batch_size, data_shape) pred_array = [] for preds, i_batch, batch in mod.iter_predict(test_rec, reset=False): for batch_index, (pred, label) in enumerate( zip(preds[0].asnumpy(), batch.label[0].asnumpy())): pred_array.append(pred) pred_arrays.append(pred_array) del mod del test_rec if weights: w = np.array(weights) w = w / np.sum(w) try: preds = np.average(pred_arrays, axis=0, weights=w) except ValueError: print( 'Length of weights not compatible with number of models.') sys.exit(1) else: preds = np.average(pred_arrays, axis=0) for i in range(len(preds)): sorted_pred = heapq.nlargest(top_k, enumerate(preds[i]), key=lambda x: x[1]) results = [] for sorted_index, value in sorted_pred: results.append("%s %s" % (sorted_index, value)) result.write("%s %s %s\n" % (test_list[i][1], int(float( test_list[i][0])), ' '.join(results)))
preds = np.average(pred_arrays, axis=0) for i in range(len(preds)): sorted_pred = heapq.nlargest(top_k, enumerate(preds[i]), key=lambda x: x[1]) results = [] for sorted_index, value in sorted_pred: results.append("%s %s" % (sorted_index, value)) result.write("%s %s %s\n" % (test_list[i][1], int(float( test_list[i][0])), ' '.join(results))) model_prefix_array = [re.sub(r'-\d+$', '', model) for model in models] print(model_prefix_array) model_epoch_array = [ int(re.sub(r'^[\w\-\.]+-', '', model)) for model in models ] print(model_epoch_array) labels_txt = "model/%s-labels.txt" % model_prefix_array[ 0] # use first labels.txt in models size = functions.get_image_size(model_prefix_array[0]) imglst = "%s/images-%s-%d.lst" % (data_dir, target, size) results_log = "logs/%s-%s-results.txt" % (model_prefix, target) make_ensemble_predict_results(data_dir, target, batch_size, imglst, labels_txt, results_log, top_k, gpus) print("Saved predict results to \"%s\"" % results_log)
from PIL import Image import os import numpy as np import pyprind from functions import get_image_size with open("./word.txt", "r") as f: words = f.read().split(",") words = [word.strip("\n") for word in words] for index, word in enumerate(words): dir_path = "./Images/{}".format(word) image_names = os.listdir(dir_path) bar = pyprind.ProgBar(len(image_names), track_time=True, title="Image {}".format(word)) for j, image_name in enumerate(image_names): bar.update() if "jpg" not in image_name: continue image_path = dir_path + "/" + image_name image = Image.open(image_path) image_size = get_image_size(reversed=True) resized_size = [image_size[0], image_size[1]] resized_image = image.resize(resized_size, Image.ANTIALIAS) resized_image.save("{}/{}.jpg".format(dir_path, j)) os.remove(image_path)