def train(): path = "./submission/snapshots/training/" images = {} i = 0 number_correct = 0 for root, dirs, filenames in os.walk(path): for f in filenames: #if f=="104lady.png": if i < 9000: #if "tree" in f: print "Loading image", f images[f] = classifier.load_image(path+f) i = i+1 f = None if WRITE_TO_FILE: f = open("training_results.txt", "w") for image in images: print "\n--------------------------------------------- PARSING IMAGE", image image_list = images[image] edge_array = image_list[0] segments = get_segments_from_edges(edge_array) x_ranges = find_object_x_ranges(segments) obj_num = 1 for range in x_ranges: obj_num +=1 print "------------------ IDENTIFYING OBJECT %s OF %s ------------------" % (obj_num, len(x_ranges)) range_segments = find_segments_in_range(range, segments) total_segment_length = length_of_all_segments(range_segments) if len(range_segments) <= 1: print "Object disregarded, only contains one path" elif total_segment_length < 50: print "Object disregarded, segments not long enough" else: best_guess = single_object_classifier(image, range_segments) if WRITE_TO_FILE: f.write("Image: %s | Classification: %s\n" % (image, best_guess)) print "\n" if SINGLE_VALIDATION: if best_guess.lower() in image.lower(): number_correct += 1 if SINGLE_VALIDATION: print "\n--------------------------------------------- RESULTS" result_string = "Correctly identified %s/%s objects." % (number_correct, len(images)) if WRITE_TO_FILE: f.write(result_string) f.close() print result_string
import numpy as np import os, sys import classifier as cf IGM_DIR = "./images/" NN = False # モデルをロード if (len(sys.argv) > 1): if (sys.argv[1] == "CNN"): NN = True print("NN") if NN == False else print("CNN") cf.load_model(NN) for dir_name in os.listdir(IGM_DIR): dir_size = 0 ans_list = np.zeros(10) print("DIR:", dir_name, end="") for filename in os.listdir(IGM_DIR + dir_name): x = cf.load_image(IGM_DIR + dir_name + "/" + filename, NN) ans = cf.classifier(x) ans_list[ans] += 1 dir_size += 1 maxIndex = [i for i, x in enumerate(ans_list) if x == max(ans_list)] print("-> Most:", maxIndex) print("{0}:".format(dir_size), ans_list)