return testpreds, testgt if __name__ == '__main__': try: config_dirs_file = sys.argv[1] # directories file config_file = sys.argv[2] # main params file except: print("Config file names not specified, setting them to default namess") config_dirs_file = "config_dirs.json" config_file = "config760.json" print(f'USING CONFIG FILES: config dirs:{config_dirs_file} main config:{config_file}') C = cs760.loadas_json(config_file) assert C["s2_classifier_type"] in ['softmax', 'sigmoid'], f"ERROR Invalid s2_classifier_type {C['s2_classifier_type']}. Must be one of 'softmax' or 'sigmoid'." if C["s2_classifier_type"] == 'sigmoid': pred_sign = sys.argv[3] # sign to predict if binary classifier assert pred_sign in C["sign_classes"], "ERROR: Invalid sign to predict: {pred_sign}." C["curr_sign"] = pred_sign print(f"TRAINING BINARY CLASSIFIER for sign {C['curr_sign']}") print("Running with parameters:", C) Cdirs = cs760.loadas_json(config_dirs_file) print("Directories:", Cdirs) C['dirs'] = Cdirs classes_dict = {}
import copy import shutil import cs760 #opencv based utils for vid / image manipulation plus other utilities try: video_directory = sys.argv[1] # intput video files feature_directory = sys.argv[2] # output feature files except: print("Video and feature directories not specified, setting them to default locations") print("Video locations: ../dataset/videos") print("Feature locations: ../features") video_directory = "../dataset/videos" feature_directory = "../features" C = cs760.loadas_json('config760.json') print("Running with parameters:", C) traindir = os.path.join(feature_directory, "train") valdir = os.path.join(feature_directory, "val") testdir = os.path.join(feature_directory, "test") print(f'Creating TRAIN Dir: {traindir}') os.makedirs(traindir, exist_ok=True) #if dir already exists will continue and WILL NOT delete existing files in that directory print(f'Creating VAL Dir: {valdir}') os.makedirs(valdir, exist_ok=True) #if dir already exists will continue and WILL NOT delete existing files in that directory print(f'Creating TEST Dir: {testdir}') os.makedirs(testdir, exist_ok=True) #if dir already exists will continue and WILL NOT delete existing files in that directory
if __name__ == '__main__': try: config_dirs_file = sys.argv[1] # directories file config_file = sys.argv[2] # main params file except: print("Config file names not specified, setting them to default namess") config_dirs_file = "config_dirs.json" config_file = "config760.json" print(f'USING CONFIG FILES: config dirs:{config_dirs_file} main config:{config_file}') #print(type(feature_directory)) C = cs760.loadas_json('config760.json') print("Running with parameters:", C) Cdirs = cs760.loadas_json(config_dirs_file) print("Directories:", Cdirs) C['dirs'] = Cdirs pkl_directory = C['dirs']['dict_pkls'] #feature_directory = C['dirs']['dict_pkls'] traindir = os.path.join(C['dirs']["dict_pkls"], "train") valdir = os.path.join(C['dirs']["dict_pkls"], "val") testdir = os.path.join(C['dirs']["dict_pkls"], "test")
def main(): try: config_dirs_file = sys.argv[1] # directories file config_file = sys.argv[2] # main params file except: print("Config file names not specified, setting them to default namess") config_dirs_file = "config_dirs.json" config_file = "config760.json" print(f'USING CONFIG FILES: config dirs:{config_dirs_file} main config:{config_file}') #print(type(feature_directory)) C = cs760.loadas_json('config760.json') print("Running with parameters:", C) Cdirs = cs760.loadas_json(config_dirs_file) print("Directories:", Cdirs) C['dirs'] = Cdirs video_directory = C['dirs']['indir'] feature_directory = C['dirs']['outdir'] print(f'Creating feature file Dir: {feature_directory}') os.makedirs(feature_directory, exist_ok=True) #if dir already exists will continue and WILL NOT delete existing files in that directory sometimes = lambda aug: iaa.Sometimes(C["augmentation_chance"][0], aug) sequential_list = [iaa.Sequential([sometimes(iaa.Fliplr(1.0))]), # horizontal flip iaa.Sequential([sometimes(iaa.Rotate(-5, 5))]), # rotate 5 degrees +/- iaa.Sequential([sometimes(iaa.CenterCropToAspectRatio(1.15))]), iaa.Sequential([sometimes(iaa.MultiplyBrightness((2.0, 2.0)))]), # increase brightness iaa.Sequential([sometimes(iaa.MultiplyHue((0.5, 1.5)))]), # change hue random iaa.Sequential([sometimes(iaa.RemoveSaturation(1.0))]), # effectively greyscale iaa.Sequential([sometimes(iaa.pillike.FilterContour())]), # edge detection iaa.Sequential([sometimes(iaa.AdditiveLaplaceNoise(scale=0.05*255, per_channel=True))]), # add colourful noise iaa.Sequential([sometimes(iaa.Invert(1))]) # invert colours ] print("Reading videos from " + video_directory) print("Outputting features to " + feature_directory) print("Loading pretrained CNN...") model = hub.KerasLayer(C["module_url"]) # can be used like any other kera layer including in other layers... print("Pretrained CNN Loaded OK") vids = cs760.list_files_pattern(video_directory, C["vid_type"]) print(f'Processing {len(vids)} videos...') for i, vid in enumerate(vids): print(f'{i} Processing: {vid}') vid_np = cs760.get_vid_frames(vid, video_directory, writejpgs=False, writenpy=False, returnnp=True) (framecount, frameheight, framewidth, channels) = vid_np.shape res_key = str(frameheight) + "-" + str(framewidth) #print(vid, vid_np.shape) outfile = os.path.splitext(vid)[0] print(f"Vid frames, h, w, c = {(framecount, frameheight, framewidth, channels)}") if C["crop_by_res"].get(res_key) is not None: vid_np_top = cs760.crop_image(vid_np, C["crop_by_res"][res_key]) print(f"Cropped by resolution to {C['crop_by_res'][res_key]}") else: vid_np_top = cs760.crop_image(vid_np, C["crop_top"]) print(f"Cropped by default to {C['crop_top']}") outfile_top = outfile + "__TOP.pkl" for n in range((len(sequential_list) + 1)): if n != 0: vid_aug = sequential_list[n - 1](images=vid_np_top) # augments frames if type(vid_aug) is list: vid_aug = np.asarray(vid_aug) batch = cs760.resize_batch(vid_aug, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L', inter=cv2.INTER_CUBIC, BGRtoRGB=False, simplenormalize=True, imagenetmeansubtract=False) temp_outfile = outfile_top[:-4] + C["augmentation_type"][n - 1] + ".pkl" features = extract(C, model, batch) cs760.saveas_pickle(features, os.path.join(feature_directory, temp_outfile)) else: batch = cs760.resize_batch(vid_np_top, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L', inter=cv2.INTER_CUBIC, BGRtoRGB=False, simplenormalize=True, imagenetmeansubtract=False) features = extract(C, model, batch) cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_top)) print(f'Features output shape: {features.shape}') if C["crop_type"] == 'B': # only for boston vids vid_np_bot = cs760.crop_image(vid_np, C["crop_bottom"]) outfile_bot = outfile + "__BOT.pkl" batch = cs760.resize_batch(vid_np_bot, width=C["expect_img_size"], height=C["expect_img_size"], pad_type='L', inter=cv2.INTER_CUBIC, BGRtoRGB=False, simplenormalize=True, imagenetmeansubtract=False) features = extract(C, model, batch) cs760.saveas_pickle(features, os.path.join(feature_directory, outfile_bot)) print('Finished outputting features!!')