def save(self): # if self. if not self.isInit: save_dir = QtGui.QFileDialog.getExistingDirectory( None, 'Select a folder to save the result', self.default_dir, QtGui.QFileDialog.ShowDirsOnly) self.isInit = True self.save_dir = str(save_dir) utils.mkdirs(self.save_dir) self.html = image_save.ImageSave(self.save_dir, 'Gui screenshot', append=True) print('save the result to (%s)' % self.save_dir) if self.z is not None: self.z_dir = os.path.join(self.save_dir, 'z_vectors') utils.mkdirs(self.z_dir) utils.PickleSave( os.path.join( self.z_dir, 'z_drawing%3.3d_%3.3d' % (self.reset_count, self.save_count)), self.z) if self.ims is not None: txts = [''] * self.ims.shape[0] self.html.save_image( self.ims, txts=txts, header='generated images (Drawing %3.3d, Step %3.3d)' % (self.reset_count, self.save_count), cvt=True, width=128) self.html.save() self.save_count += 1
def __init__(self, model_name): self.isInit = False self.model_name = model_name self.default_dir = os.path.join('./web/', model_name) utils.mkdirs(self.default_dir) self.reset_count = 0 self.save_count = 0
def get_keywords_urls(keyword, out_dir, limit): page_num = 0 mkdirs(out_dir) out_path = os.path.join(out_dir, keyword + '.txt') hash_word = urllib.quote(keyword) while page_num < limit: url_list = get_page_urls(page_num, hash_word) save_per_page_img_url(url_list, out_path) page_num += 50 if len(url_list) == 0: break time.sleep(1) return page_num
def download_page_by_url(url): global Image_download_count image_type = 'img' try: req = urllib2.Request(url, headers=header) raw_img = urllib2.urlopen(req).read() filesize = raw_img.__sizeof__() print 'file size : ',filesize if filesize > filtersize: filename = image_type + "_"+ str(Image_download_count) Image_download_count += 1 print 'downloaded : '+filename + ' : ' + url mkdirs(OutputDir) f = open(os.path.join(OutputDir,filename+".jpg"), 'wb') f.write(raw_img) f.close() except Exception as e: print "could not load : "+url print e
def visualize_recon(self): one_batch = next(iter(self.data_loader)) x_true, _ = one_batch assert len(x_true)>=4, 'set the batch size bigger than 4' x_true = x_true[0:5] x_recon,_,_,_,_ = self.model.module.forward(x_true.cuda()) if self.model.module.output_type == 'binary': x_recon = torch.sigmoid(x_recon) x_true = x_true.view(x_recon.shape) N = x_true.shape[0] fused_images = torch.zeros((2*N,self.nc,x_recon.shape[2],x_recon.shape[3])) fused_images[0:2*N-1:2] = x_true fused_images[1:2*N:2] = x_recon mkdirs(os.path.join(self.output_dir,"images")) output_dir = os.path.join(self.output_dir,"images/reconstruction.jpg") if self.nc ==3: fused_images = fused_images * 255 save_image(fused_images.cpu(), output_dir,nrow=2, pad_value=1)
def save(self): # if self. if not self.isInit: save_dir = QtGui.QFileDialog.getExistingDirectory(None, 'Select a folder to save the result', self.default_dir,QtGui.QFileDialog.ShowDirsOnly) self.isInit = True self.save_dir = str(save_dir) utils.mkdirs(self.save_dir) self.html = image_save.ImageSave(self.save_dir, 'Gui screenshot', append=True) print('save the result to (%s)' % self.save_dir) if self.z is not None: self.z_dir = os.path.join(self.save_dir, 'z_vectors') utils.mkdirs(self.z_dir) utils.PickleSave(os.path.join(self.z_dir, 'z_drawing%3.3d_%3.3d' %(self.reset_count, self.save_count)), self.z) if self.ims is not None: txts=['']*self.ims.shape[0] self.html.save_image(self.ims, txts=txts, header='generated images (Drawing %3.3d, Step %3.3d)' % (self.reset_count, self.save_count), cvt=True, width=128) self.html.save() self.save_count += 1
train_dcgan_config, args.model_name)() expr_name = args.model_name + args.ext if not args.cache_dir: args.cache_dir = './cache/%s/' % expr_name for arg in vars(args): print('[%s] =' % arg, getattr(args, arg)) # create directories rec_dir = os.path.join(args.cache_dir, 'rec') model_dir = os.path.join(args.cache_dir, 'models') log_dir = os.path.join(args.cache_dir, 'log') web_dir = os.path.join(args.cache_dir, 'web_rec') html = image_save.ImageSave(web_dir, expr_name, append=True) utils.mkdirs([rec_dir, model_dir, log_dir, web_dir]) # load data tr_data, te_data, tr_stream, te_stream, ntrain, ntest \ = load_imgs(ntrain=None, ntest=None, batch_size=args.batch_size, data_file=args.data_file) te_handle = te_data.open() ntest = int(np.floor(ntest / float(args.batch_size)) * args.batch_size) # st() test_x, = te_data.get_data(te_handle, slice(0, ntest)) test_x = train_dcgan_utils.transform(test_x, nc=nc) predict_params = train_dcgan_utils.init_predict_params(nz=nz, n_f=n_f, n_layers=n_layers, nc=nc) # load modelG
def main(args): #Extracting the information from the configuration file mode = config.get('general', 'mode') nb_frames = config.getint('general', 'nb_frames') skip = config.getint('general', 'skip') target_size = literal_eval(config.get('general', 'target_size')) batch_size = config.getint('general', 'batch_size') epochs = config.getint('general', 'epochs') nb_classes = config.getint('general', 'nb_classes') model_name = config.get('path', 'model_name') data_root = config.get('path', 'data_root') data_model = config.get('path', 'data_model') data_vid = config.get('path', 'data_vid') path_weights = config.get('path', 'path_weights') csv_labels = config.get('path', 'csv_labels') csv_train = config.get('path', 'csv_train') csv_val = config.get('path', 'csv_val') csv_test = config.get('path', 'csv_test') #Joining together the needed paths path_vid = os.path.join(data_root, data_vid) path_model = os.path.join(data_root, data_model, model_name) path_labels = os.path.join(data_root, csv_labels) path_train = os.path.join(data_root, csv_train) path_val = os.path.join(data_root, csv_val) path_test = os.path.join(data_root, csv_test) #Input shape of the input Tensor inp_shape = (nb_frames, ) + target_size + (3, ) if mode == 'train': data = DataLoader(path_vid, path_labels, path_train, path_val) #Creating the model and graph folder mkdirs(path_model, 0o755) mkdirs(os.path.join(path_model, "graphs"), 0o755) #Creating the generators for the training and validation set gen = kmg.ImageDataGenerator() gen_train = gen.flow_video_from_dataframe(data.train_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="label", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) gen_val = gen.flow_video_from_dataframe(data.val_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="label", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) net = Resnet3DBuilder.build_resnet_101(inp_shape, nb_classes, drop_rate=0.5) opti = SGD(lr=0.01, momentum=0.9, decay=0.0001, nesterov=False) net.compile(optimizer=opti, loss="categorical_crossentropy", metrics=["accuracy"]) if (path_weights != "None"): print("Loading weights from : " + path_weights) net.load_weights(path_weights) model_file_format_best = os.path.join(path_model, 'model.best.hdf5') checkpointer_best = ModelCheckpoint(model_file_format_best, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max') history_graph = HistoryGraph( model_path_name=os.path.join(path_model, "graphs")) #Get the number of sample in the training and validation set nb_sample_train = data.train_df["video_id"].size nb_sample_val = data.val_df["video_id"].size #Launch the training net.fit( gen_train, steps_per_epoch=ceil(nb_sample_train / batch_size), epochs=epochs, validation_data=gen_val, validation_steps=ceil(nb_sample_val / batch_size), shuffle=True, verbose=1, callbacks=[checkpointer_best, history_graph], ) elif mode == 'test': data = DataLoader(path_vid, path_labels, path_test=path_test) gen = kmg.ImageDataGenerator() gen_test = gen.flow_video_from_dataframe(data.test_df, path_vid, shuffle=False, path_classes=path_labels, class_mode=None, x_col='video_id', target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) #Building model net = Resnet3DBuilder.build_resnet_101(inp_shape, nb_classes) if (path_weights != "None"): print("Loading weights from : " + path_weights) net.load_weights(path_weights) else: sys.exit( "<Error>: Specify a value for path_weights different from None when using test mode" ) #Get the number of sample in the test set nb_sample_test = data.test_df["video_id"].size res = net.predict(gen_test, steps=ceil(nb_sample_test / batch_size), verbose=1) #Create an empty column called label data.test_df['label'] = "" #For each result get the string label and set it in the DataFrame for i, item in enumerate(res): item[item == np.max(item)] = 1 item[item != np.max(item)] = 0 label = data.categorical_to_label(item) data.test_df.at[i, 'label'] = label #Faster than iloc #Save the resulting DataFrame to a csv data.test_df.to_csv(os.path.join(path_model, "prediction.csv"), sep=';', header=False, index=False) else: sys.exit("<Error>: Use either {train,test} mode")
train_dcgan_config, args.model_name)() expr_name = args.model_name + args.ext if not args.cache_dir: args.cache_dir = './cache/%s/' % expr_name for arg in vars(args): print('[%s] =' % arg, getattr(args, arg)) # create directories sample_dir = os.path.join(args.cache_dir, 'samples') model_dir = os.path.join(args.cache_dir, 'models') log_dir = os.path.join(args.cache_dir, 'log') web_dir = os.path.join(args.cache_dir, 'web_dcgan') html = image_save.ImageSave(web_dir, expr_name, append=True) utils.mkdirs([sample_dir, model_dir, log_dir, web_dir]) # load data from hdf5 file tr_data, te_data, tr_stream, te_stream, ntrain, ntest = load.load_imgs( ntrain=None, ntest=None, batch_size=args.batch_size, data_file=args.data_file) te_handle = te_data.open() test_x, = te_data.get_data(te_handle, slice(0, ntest)) # generate real samples and test transform/inverse_transform test_x = train_dcgan_utils.transform(test_x, nc=nc) vis_idxs = py_rng.sample(np.arange(len(test_x)), n_vis) vaX_vis = train_dcgan_utils.inverse_transform(test_x[vis_idxs], npx=npx, nc=nc) # st()
def __init__(self, args): self.data_loader = return_data(args) self.model_name = args.model self.z_dim = args.z_dim self.nc = args.number_channel # if args.dataset == 'mnist': # assert os.path.exists('./{}_model_mnist.pk'.format(self.model_name)), 'no model found!' # from models.beta_vae_archs import Beta_VAE_mnist # self.model = nn.DataParallel(Beta_VAE_mnist(z_dim=args.z_dim,n_cls=10).cuda(), device_ids=range(1)) # self.model.load_state_dict(torch.load('./{}_model_mnist.pk'.format(self.model_name))) # dim = 28 # elif args.dataset == 'dsprites': # assert os.path.exists('./{}_model_dsprites.pk'.format(self.model_name)), 'no model found!' # if args.loss_fun != 'TC_montecarlo': # from models.beta_vae_archs import Beta_VAE_Burgess # self.model = nn.DataParallel(Beta_VAE_Burgess(z_dim=args.z_dim, n_cls=3, # computes_std=args.computes_std).cuda(), device_ids=range(1)) # else: # from models.beta_vae_archs import Beta_VAE_Burgess # self.model = nn.DataParallel(Beta_VAE_Burgess(z_dim=args.z_dim,n_cls=3).cuda(), device_ids=range(1)) # dim = 64 # self.model.load_state_dict(torch.load('./{}_model_{}.pk'.format(args.model,args.dataset))) # else: # assert os.path.exists('./betavae_model_3Dshapes.pk'), 'no model found!' # from models.beta_vae_archs import Beta_VAE_BN # self.model = nn.DataParallel(Beta_VAE_BN(z_dim=args.z_dim,nc=3, n_cls=4, # output_type='continues').cuda(), device_ids=range(1)) # self.model.load_state_dict(torch.load('./betavae_model_3Dshapes.pk')) # dim = 64 # self.nc =3 # assert os.path.exists('./{}_model_{}.pk'.format(self.model_name,self.dataset)), 'no model found!' # from models.beta_vae_archs import Beta_VAE_Burgess assert os.path.exists('./{}_model_{}.pk'.format(self.model_name,args.dataset)), 'no model found!' mod = __import__('models.beta_vae_archs', fromlist=['Beta_VAE_{}'.format(args.arch)]) self.model = nn.DataParallel(getattr(mod, 'Beta_VAE_{}'.format(args.arch))(z_dim=args.z_dim, n_cls=args.n_cls, computes_std=args.computes_std, nc=args.number_channel, output_type= args.output_type).cuda(), device_ids=range(1)) dim = args.image_size self.model.load_state_dict(torch.load('./{}_model_{}.pk'.format(args.model,args.dataset))) self.output_dir = os.path.join(args.output_dir) if args.name != 'main': self.todays_date = args.name else: self.todays_date = datetime.datetime.today() self.output_dir = os.path.join(self.output_dir, str(self.todays_date)) if not os.path.exists(self.output_dir): mkdirs(self.output_dir) self.device = 'cuda' self.dataset = args.dataset self.output_save = args.output_save if args.dataset == "mnist": self.width = 28 self.height = 28 else: self.width = 64 self.height = 64 if os.path.exists('./AVG_KLDs_VAR_means.pth'): AVG_KLDs_var_means = torch.load('./AVG_KLDs_VAR_means.pth') self.AVG_KLDs = AVG_KLDs_var_means['AVG_KLDs'] self.VAR_means = AVG_KLDs_var_means['VAR_means'] else: self.model.module.eval() print('computing average of KLDs and variance of means of latent units.') N = len(self.data_loader) dataset_size = N * args.batch_size AVG_KLD = torch.zeros(self.model.module.z_dim).cpu() #self.model.eval() means = [] for i ,(x, y) in tqdm(enumerate(self.data_loader, 0),total=len(self.data_loader),smoothing=0.9): x = x.cuda(async=True) params = self.model.module.encoder(x).view(x.size(0),self.z_dim,2) mu, logstd_var = params.select(-1,0), params.select(-1,1) KLDs = self.model.module.kld_unit_guassians_per_sample(mu, logstd_var) AVG_KLD += KLDs.sum(0).cpu().detach() means.append(mu.cpu().detach()) self.AVG_KLDs = AVG_KLD/ dataset_size self.VAR_means = torch.std(torch.cat(means),dim=0).pow(2) self.sorted_idx_KLDs = torch.argsort(self.AVG_KLDs,descending=True) self.sorted_idx_VAR_means = torch.argsort(self.AVG_KLDs,descending=True)
npx, n_layers, n_f, nc, nz, niter, niter_decay = getattr(train_dcgan_config, args.model_name)() expr_name = args.model_name + args.ext if not args.cache_dir: args.cache_dir = './cache/%s/' % expr_name for arg in vars(args): print('[%s] =' % arg, getattr(args, arg)) # create directories rec_dir = os.path.join(args.cache_dir, 'rec') model_dir = os.path.join(args.cache_dir, 'models') log_dir = os.path.join(args.cache_dir, 'log') web_dir = os.path.join(args.cache_dir, 'web_rec') html = image_save.ImageSave(web_dir, expr_name, append=True) utils.mkdirs([rec_dir, model_dir, log_dir, web_dir]) # load data tr_data, te_data, tr_stream, te_stream, ntrain, ntest \ = load_imgs(ntrain=None, ntest=None, batch_size=args.batch_size, data_file=args.data_file) te_handle = te_data.open() ntest = int(np.floor(ntest/float(args.batch_size)) * args.batch_size) # st() test_x, = te_data.get_data(te_handle, slice(0, ntest)) test_x = train_dcgan_utils.transform(test_x, nc=nc) predict_params = train_dcgan_utils.init_predict_params(nz=nz, n_f=n_f, n_layers=n_layers, nc=nc) # load modelG gen_params = train_dcgan_utils.init_gen_params(nz=nz, n_f=n_f, n_layers=n_layers, nc=nc) train_dcgan_utils.load_model(gen_params, os.path.join(model_dir, 'gen_params')) gen_batchnorm = train_dcgan_utils.load_batchnorm(os.path.join(model_dir, 'gen_batchnorm'))
def __init__(self, args): self.data_loader = return_data(args) self.model_name = args.model self.z_dim = [args.con_latent_dims, args.disc_latent_dims] self.nc = 1 dim = args.image_size mod = __import__('models.joint_b_vae_archs', fromlist=['Joint_VAE_{}'.format(args.arch)]) self.model = nn.DataParallel(getattr(mod, 'Joint_VAE_{}'.format(args.arch))(z_dim=args.z_dim, n_cls=args.n_cls, computes_std=args.computes_std, nc=args.number_channel, output_size=(dim,dim), output_type= args.output_type).cuda(), device_ids=range(1)) self.nc =3 self.output_dir = os.path.join(args.output_dir) self.todays_date = datetime.datetime.today() self.output_dir = os.path.join(self.output_dir, str(self.todays_date)) if not os.path.exists(self.output_dir): mkdirs(self.output_dir) self.device = 'cuda' self.dataset = args.dataset self.output_save = args.output_save if args.dataset == "mnist": self.width = 32 self.height = 32 else: self.width = 64 self.height = 64 if os.path.exists('./AVG_KLDs_VAR_means.pth'): AVG_KLDs_var_means = torch.load('./AVG_KLDs_VAR_means.pth') self.AVG_KLDs = AVG_KLDs_var_means['AVG_KLDs'] self.VAR_means = AVG_KLDs_var_means['VAR_means'] else: with torch.no_grad(): self.model.module.eval() print('computing average of KLDs and variance of means of latent units.') N = len(self.data_loader) dataset_size = N * args.batch_size AVG_KLD = torch.zeros(self.model.module.z_dim[0]).cpu() #self.model.eval() means = [] print('And computing average of KLDs and variance of means of discrete latent units.') means = [] AVG_KLDs_disc = [torch.Tensor([0]) for i in range(len(self.z_dim[1]))] for i ,(x, y) in tqdm(enumerate(self.data_loader, 0),total=len(self.data_loader),smoothing=0.9): x = x.cuda(async=True) x_hat, mu, logstd_var, z, alphas, rep_as = self.model(x) loss, recon, KLD_total_mean, KLDs, KLD_catigorical_total_mean, KLDs_catigorical = \ self.model.module.losses(x, x_hat, mu, logstd_var, z, alphas, rep_as, 'H') for ii in range(len(KLDs_catigorical)): AVG_KLDs_disc[ii] += KLDs_catigorical[ii] KLDs = self.model.module.kld_unit_guassians_per_sample(mu, logstd_var) AVG_KLD += KLDs.sum(0).cpu().detach() means.append(mu.cpu().detach()) for ii in range(len(AVG_KLDs_disc)): AVG_KLDs_disc[ii] = args.batch_size *AVG_KLDs_disc[ii]/ dataset_size self.AVG_KLDs_disc = torch.cat(AVG_KLDs_disc,dim=0) self.sorted_idx_KLDs_disc = self.AVG_KLDs_disc.argsort(0) self.AVG_KLDs = AVG_KLD / dataset_size self.VAR_means = torch.std(torch.cat(means),dim=0).pow(2) self.sorted_idx_KLDs = torch.argsort(self.AVG_KLDs,descending=True) self.sorted_idx_VAR_means = torch.argsort(self.AVG_KLDs,descending=True) mkdirs(os.path.join(self.output_dir,"images"))
def main_predict_on_thread(): config = configparser.ConfigParser() config.read('config.cfg') args = config #Extracting the information from the configuration file mode = config.get('general', 'mode') nb_frames = config.getint('general', 'nb_frames') skip = config.getint('general', 'skip') target_size = literal_eval(config.get('general', 'target_size')) batch_size = config.getint('general', 'batch_size') epochs = config.getint('general', 'epochs') nb_classes = config.getint('general', 'nb_classes') model_name = config.get('path', 'model_name') data_root = config.get('path', 'data_root') data_model = config.get('path', 'data_model') data_vid = config.get('path', 'data_vid') path_weights = config.get('path', 'path_weights') csv_labels = config.get('path', 'csv_labels') csv_train = config.get('path', 'csv_train') csv_val = config.get('path', 'csv_val') csv_test = config.get('path', 'csv_test') workers = config.getint('option', 'workers') use_multiprocessing = config.getboolean('option', 'use_multiprocessing') max_queue_size = config.getint('option', 'max_queue_size') #Joining together the needed paths path_vid = os.path.join(data_root, data_vid) path_model = os.path.join(data_root, data_model) path_labels = os.path.join(data_root, csv_labels) path_train = os.path.join(data_root, csv_train) path_val = os.path.join(data_root, csv_val) path_test = os.path.join(data_root, csv_test) #Input shape of the input Tensor #inp_shape = (None, None, None, 3) inp_shape = (nb_frames, ) + target_size + (3, ) img_show_camera = True if mode == 'train': data = DataLoader(path_vid, path_labels, path_train, path_val) #Creating the model and graph folder mkdirs(path_model, 0o755) mkdirs(os.path.join(path_model, "graphs"), 0o755) #Creating the generators for the training and validation set gen = kmg.ImageDataGenerator() gen_train = gen.flow_video_from_dataframe(data.train_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="label", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) gen_val = gen.flow_video_from_dataframe(data.val_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="label", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) """ #Building model net = model.CNN3D(inp_shape=inp_shape,nb_classes=nb_classes, drop_rate=0.5) #Compiling model net.compile(optimizer="Adadelta", loss="categorical_crossentropy", metrics=["accuracy", "top_k_categorical_accuracy"]) """ net = Resnet3DBuilder.build_resnet_101(inp_shape, nb_classes, drop_rate=0.5) opti = SGD(lr=0.01, momentum=0.9, decay=0.0001, nesterov=False) net.compile(optimizer=opti, loss="categorical_crossentropy", metrics=["accuracy"]) if (path_weights != "None"): print("Loading weights from : " + path_weights) net.load_weights(path_weights) #model_file_format_last = os.path.join(path_model,'model.{epoch:03d}.hdf5') model_file_format_best = os.path.join(path_model, 'model.best.hdf5') checkpointer_best = ModelCheckpoint(model_file_format_best, monitor='val_acc', verbose=1, save_best_only=True, mode='max') #checkpointer_last = ModelCheckpoint(model_file_format_last, period=1) history_graph = HistoryGraph( model_path_name=os.path.join(path_model, "graphs")) #Get the number of sample in the training and validation set nb_sample_train = data.train_df["video_id"].size nb_sample_val = data.val_df["video_id"].size #Launch the training net.fit_generator( generator=gen_train, steps_per_epoch=ceil(nb_sample_train / batch_size), epochs=epochs, validation_data=gen_val, validation_steps=ceil(nb_sample_val / batch_size), shuffle=True, verbose=1, workers=workers, max_queue_size=max_queue_size, use_multiprocessing=use_multiprocessing, callbacks=[checkpointer_best, history_graph], ) elif mode == 'test': data = DataLoader(path_vid, path_labels, path_test=path_test) gen = kmg.ImageDataGenerator() gen_test = gen.flow_video_from_dataframe(data.test_df, path_vid, shuffle=False, path_classes=path_labels, class_mode=None, x_col='video_id', target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) #test flow data co = 0 for fl in gen_test: co += 1 if co > 10: break print(fl.shape) for i in range(16): cv2.imshow('frame', fl[0][i]) cv2.waitKey() exit() #Building model net = Resnet3DBuilder.build_resnet_50(inp_shape, nb_classes) if (path_weights != "None"): print("Loading weights from : " + path_weights) net.load_weights(path_weights) else: sys.exit( "<Error>: Specify a value for path_weights different from None when using test mode" ) #Get the number of sample in the test set nb_sample_test = data.test_df["video_id"].size res = net.predict_generator( generator=gen_test, steps=ceil(nb_sample_test / batch_size), verbose=1, workers=workers, use_multiprocessing=use_multiprocessing, ) #Create an empty column called label data.test_df['label'] = "" #For each result get the string label and set it in the DataFrame for i, item in enumerate(res): item[item == np.max(item)] = 1 item[item != np.max(item)] = 0 label = data.categorical_to_label(item) #data.test_df.iloc[i,data.test_df.columns.get_loc('label')] = label data.test_df.at[i, 'label'] = label #Faster than iloc #Save the resulting DataFrame to a csv data.test_df.to_csv(os.path.join(path_model, "prediction.csv"), sep=';', header=False, index=False) elif mode == 'video': label_res = '' skip, fr, delay_ = 2, 0, 5 data_frame = [] data_map = DataLoader(path_vid, path_labels, path_test=path_test) #build model net = Resnet3DBuilder.build_resnet_50(inp_shape, nb_classes) net.load_weights(path_weights) print('load model done') #open camera cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. #out = cv2.VideoWriter('gao_video.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 24, (int(cap.get(3)),int(cap.get(4)))) ret, frame = cap.read() last_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) while (cap.isOpened()): #show video ret, frame = cap.read() #xu ly tru nen phat hien khoang lang gray = cv2.cvtColor( frame, cv2.COLOR_BGR2GRAY) # convert color image to gray gray = cv2.GaussianBlur(gray, (5, 5), 0) diff = cv2.absdiff(gray, last_gray) # frame difference! last_gray = gray thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) print('subtraction frame', np.mean(thresh)) if img_show_camera: cv2.putText(frame, label_res, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 2) cv2.imshow('frame', frame) cv2.imshow('diff', diff) cv2.imshow('thresh', thresh) if cv2.waitKey(1) & 0xFF == ord('q'): break continue #process image to push to data_frame to gesture regconition frameRGB = frame # cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frameRGB = cv2.resize(frameRGB, (target_size[1], target_size[0]), interpolation=cv2.INTER_NEAREST) #get frame to data_frame fr += 1 delay_ -= 1 if fr % skip and delay_ < 0: data_frame.append(frameRGB) if len(data_frame) == 16: t1 = time.time() data_frame = [data_frame] data_frame = np.array(data_frame) / 255 res = net.predict(data_frame) prob = np.max(res) for i, item in enumerate(res): item[item == np.max(item)] = 1 item[item != np.max(item)] = 0 label = data_map.categorical_to_label(item) if prob >= 0.6: label_res = label print(label, prob) #reset data_frame delay_ = 5 data_frame = [] print('video/s:', 1 / (time.time() - t1)) # When everything done, release the video capture and video write objects cap.release() #out.release() cv2.destroyAllWindows() else: sys.exit("<Error>: Use either {train,test} mode")
npx, n_layers, n_f, nc, nz, niter, niter_decay = getattr(train_dcgan_config, args.model_name)() expr_name = args.model_name + args.ext if not args.cache_dir: args.cache_dir = './cache/%s/' % expr_name for arg in vars(args): print('[%s] =' % arg, getattr(args, arg)) # create directories sample_dir = os.path.join(args.cache_dir, 'samples') model_dir = os.path.join(args.cache_dir, 'models') log_dir = os.path.join(args.cache_dir, 'log') web_dir = os.path.join(args.cache_dir, 'web_dcgan') html = image_save.ImageSave(web_dir, expr_name, append=True) utils.mkdirs([sample_dir, model_dir, log_dir, web_dir]) # load data from hdf5 file tr_data, te_data, tr_stream, te_stream, ntrain, ntest = load.load_imgs(ntrain=None, ntest=None, batch_size=args.batch_size,data_file=args.data_file) te_handle = te_data.open() test_x, = te_data.get_data(te_handle, slice(0, ntest)) # generate real samples and test transform/inverse_transform test_x = train_dcgan_utils.transform(test_x, nc=nc) vis_idxs = py_rng.sample(np.arange(len(test_x)), n_vis) vaX_vis = train_dcgan_utils.inverse_transform(test_x[vis_idxs], npx=npx, nc=nc) # st() n_grid = int(np.sqrt(n_vis)) grid_real = utils.grid_vis((vaX_vis*255.0).astype(np.uint8), n_grid, n_grid) train_dcgan_utils.save_image(grid_real, os.path.join(sample_dir, 'real_samples.png'))
def main(args): # extract information from the configuration file nb_frames = config.getint('general', 'nb_frames') skip = config.getint('general', 'skip') target_size = literal_eval(config.get('general', 'target_size')) batch_size = config.getint('general', 'batch_size') epochs = config.getint('general', 'epochs') nb_classes = config.getint('general', 'nb_classes') model_name = config.get('path', 'model_name') data_root = config.get('path', 'data_root') data_model = config.get('path', 'data_model') data_vid = config.get('path', 'data_vid') path_weights = config.get('path', 'path_weights') csv_labels = config.get('path', 'csv_labels') csv_train = config.get('path', 'csv_train') csv_val = config.get('path', 'csv_val') workers = config.getint('option', 'workers') use_multiprocessing = config.getboolean('option', 'use_multiprocessing') max_queue_size = config.getint('option', 'max_queue_size') # join together the needed paths path_vid = os.path.join(data_root, data_vid) path_model = os.path.join(data_root, data_model, model_name) path_labels = os.path.join(data_root, csv_labels) path_train = os.path.join(data_root, csv_train) path_val = os.path.join(data_root, csv_val) # Input shape of the input Tensor inp_shape = (nb_frames, ) + target_size + (3, ) # load the data using DataLoader class data = DataLoader(path_vid, path_labels, path_train, path_val) # create model folder mkdirs(path_model, 0o755) # create the generators for the training and validation set gen = kmg.ImageDataGenerator() gen_train = gen.flow_video_from_dataframe(data.train_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="label", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) gen_val = gen.flow_video_from_dataframe(data.val_df, path_vid, path_classes=path_labels, x_col='video_id', y_col="label", target_size=target_size, batch_size=batch_size, nb_frames=nb_frames, skip=skip, has_ext=True) # MODEL # # Build and compile RESNET3D model # net = Resnet3DBuilder.build_resnet_101(inp_shape, nb_classes, drop_rate=0.5) # opti = SGD(lr=0.01, momentum=0.9, decay= 0.0001, nesterov=False) # net.compile(optimizer=opti, # loss="categorical_crossentropy", # metrics=["accuracy"]) # Build and compile RadhaKrishna model net = model.RadhaKrishna(inp_shape=inp_shape, nb_classes=nb_classes) net.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy", "top_k_categorical_accuracy"]) # if model weights file is present # load the model weights if (path_weights != "None"): print("Loading weights from : " + path_weights) net.load_weights(path_weights) # file format for saving the best model # model_file_format_best = os.path.join(path_model,'radhakrishna.hdf5') # save checkpoint checkpoint_path = "training_1/cp.ckpt" checkpoint_dir = os.path.dirname(checkpoint_path) # checkpoint the best model # checkpointer_best = ModelCheckpoint(model_file_format_best, monitor='val_accuracy',verbose=1, save_best_only=True, mode='max') # checkpointer_best = ModelCheckpoint(filepath=os.path.join(path_model, 'model.{epoch:02d}-{val_loss:.2f}.h5'), monitor='val_accuracy',verbose=1, save_best_only=True, mode='max') es = EarlyStopping(monitor='val_loss', mode='min', verbose=1) # Create a callback that saves the model's weights cp_callback = keras.callbacks.ModelCheckpoint(filepath=checkpoint_path, monitor='val_accuracy', save_weights_only=True, verbose=1) # get the number of samples in the training and validation set nb_sample_train = data.train_df["video_id"].size nb_sample_val = data.val_df["video_id"].size # launch the training net.fit_generator( generator=gen_train, steps_per_epoch=ceil(nb_sample_train / batch_size), epochs=epochs, validation_data=gen_val, validation_steps=ceil(nb_sample_val / batch_size), shuffle=True, verbose=1, workers=workers, max_queue_size=max_queue_size, use_multiprocessing=use_multiprocessing, callbacks=[cp_callback, es], ) model.save_weights('./checkpoints/radhakrishna') # after training serialize the final model to JSON model_json = net.to_json() with open(model_name + ".json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 net.save_weights(model_name + ".h5") print("Saved model to disk")