def __init__(self, cfg, writer=None): super(TRecgNet, self).__init__(cfg) util.mkdir(self.save_dir) self.phase = cfg.PHASE self.trans = not cfg.NO_TRANS self.content_model = None self.batch_size = cfg.BATCH_SIZE self.writer = writer # networks # self.net = networks.define_netowrks(cfg, device=self.device) from lib.sync_bn.modules import BatchNorm2d as SyncBatchNorm self.net = networks.define_netowrks(cfg, device=self.device, BatchNorm=SyncBatchNorm) self.modules_ori = [ self.net.layer0, self.net.layer1, self.net.layer2, self.net.layer3, self.net.layer4 ] self.modules_new = [self.net.ppm, self.net.cls, self.net.aux] self.params_list = [] for module in self.modules_ori: self.params_list.append(dict(params=module.parameters(), lr=cfg.LR)) for module in self.modules_new: self.params_list.append( dict(params=module.parameters(), lr=cfg.LR * 10)) networks.print_network(self.net)
def write_results(config: configure_finetuning.FinetuningConfig, results): """Write evaluation metrics to disk.""" utils.log("Writing results to", config.results_txt) utils.mkdir(config.results_txt.rsplit("/", 1)[0]) utils.write_pickle(results, config.results_pkl) with tf.io.gfile.GFile(config.results_txt, "a") as f: results_str = "" for trial_results in results: for task_name, task_results in trial_results.items(): if task_name == "time" or task_name == "global_step": continue results_str += task_name + ": " + " - ".join([ "{:}: {:.2f}".format(k, v) for k, v in task_results.items() ]) + "\n" # Neptune Metric Logging neptune.append_tag('ft') neptune.append_tag('tensorflow') neptune.set_property('task', task_name) for k, v in task_results.items(): neptune.log_metric(k, v) f.write(results_str) utils.write_pickle(results, config.results_pkl)
def write_results(config: configure_finetuning.FinetuningConfig, results): """Write evaluation metrics to disk.""" utils.log("Writing results to", config.results_txt) utils.mkdir(config.results_txt.rsplit("/", 1)[0]) utils.write_pickle(results, config.results_pkl) with tf.io.gfile.GFile(config.results_txt, "w") as f: results_str = "" for trial_results in results: for task_name, task_results in trial_results.items(): if task_name == "time" or task_name == "global_step": continue results_str += task_name + ": " + " - ".join( ["{:}: {:.2f}".format(k, v) for k, v in task_results.items()]) + "\n" f.write(results_str) utils.write_pickle(results, config.results_pkl)
def save_scores(single_url, title): all_links_of_one_song = resolve_page(single_url) if not all_links_of_one_song: print('No scores in this song') for imageLink in all_links_of_one_song: data = get_image_data(imageLink) if data is not None: file_path = NORMAL_FOLDER + title.replace('/', '_') mkdir(file_path) image_name = imageLink.split('/')[-1] f = open(file_path + '/' + image_name, 'wb') f.write(data) f.close()
def loop_date_to_get_link(full_link, title): for i, j in itertools.product(range(2010, 2018), range(1, 13)): slices = full_link.split('/') full_link = '/'.join((slices[0], slices[1], slices[2], str(i), str('%02d' % j), slices[-1])) print('looping', end=' ... ') data = get_image_data(full_link) if data is not None: file_path = VIP_FOLDER + title.replace('/', '_') mkdir(file_path) image_name = full_link.split('/')[-1] f = open(file_path + '/' + image_name, 'wb') f.write(data) f.close() return full_link
def __init__(self, cfg, writer=None): super(TRecgNet, self).__init__(cfg) util.mkdir(self.save_dir) assert (self.cfg.WHICH_DIRECTION is not None) self.AtoB = self.cfg.WHICH_DIRECTION == 'AtoB' self.modality = 'rgb' if self.AtoB else 'depth' self.sample_model = None self.phase = cfg.PHASE self.upsample = not cfg.NO_UPSAMPLE self.content_model = None self.content_layers = [] self.writer = writer # networks self.use_noise = cfg.WHICH_DIRECTION == 'BtoA' self.net = networks.define_TrecgNet(cfg, device=self.device) networks.print_network(self.net)
def _serialize_balanced_dataset(self, tasks, is_training, split): """Write out the dataset as tfrecords.""" labels = [ self._tasks[i]._label_list for i in range(len(self_tasks)) ] dataset_name = "_".join(sorted([task.name for task in tasks])) dataset_name += "_" + split dataset_prefix = os.path.join(self._config.preprocessed_data_dir, dataset_name) tfrecords_path = dataset_prefix + ".tfrecord" metadata_path = dataset_prefix + ".metadata" batch_size = (self._config.train_batch_size if is_training else self._config.eval_batch_size) utils.log("Loading dataset", dataset_name) n_examples = None if (self._config.use_tfrecords_if_existing and tf.io.gfile.exists(metadata_path)): n_examples = utils.load_json(metadata_path)["n_examples"] if n_examples is None: utils.log("Existing tfrecords not found so creating") examples = [] for task in tasks: task_examples = task.get_examples(split) examples += task_examples if is_training: random.shuffle(examples) utils.mkdir(tfrecords_path.rsplit("/", 1)[0]) n_examples = self.serialize_examples(examples, is_training, tfrecords_path, batch_size) utils.write_json({"n_examples": n_examples}, metadata_path) input_fn = self._input_fn_builder(tfrecords_path, is_training) if is_training: steps = int(n_examples // batch_size * self._config.num_train_epochs) else: steps = n_examples // batch_size return input_fn, steps
def __init__(self, cfg, vis=None, writer=None): super(TRecgNet, self).__init__(cfg) util.mkdir(self.save_dir) assert (self.cfg.WHICH_DIRECTION is not None) self.AtoB = self.cfg.WHICH_DIRECTION == 'AtoB' self.modality = 'rgb' if self.AtoB else 'depth' self.sample_model = None self.phase = cfg.PHASE self.using_semantic_branch= cfg.USING_SEMANTIC_BRANCH self.content_model = None self.content_layers = [] self.batch_size=cfg.BATCH_SIZE self.writer = writer self.vis = vis # networks self.use_noise = cfg.WHICH_DIRECTION == 'BtoA' self.net = networks.define_TrecgNet(cfg, using_semantic_branch=cfg.USING_SEMANTIC_BRANCH, device=self.device) networks.print_network(self.net)
def save_scores(single_url, title): vip_link = get_possible_vip_link(single_url) for image_index in range(1, 20): full_link = vip_link.split('.gif')[0] + '_' + str(image_index) + '.gif' data = get_image_data(full_link) if data is not None: file_path = VIP_FOLDER + title.replace('/', '_') mkdir(file_path) image_name = full_link.split('/')[-1] f = open(file_path + '/' + image_name, 'wb') f.write(data) f.close() else: if image_index == 1: correct_link = loop_date_to_get_link(full_link, title) if correct_link is not None: vip_link = correct_link[:-6] + ".gif" else: break
from keras.models import Model from keras.layers.normalization import BatchNormalization from keras.callbacks import EarlyStopping, ModelCheckpoint from sklearn.preprocessing import StandardScaler from tqdm._tqdm import tqdm from util.utils import mkdir ######################################## ## set directories and parameters ######################################## BASE_DIR = '../quora-data/' timestr = strftime("%Y%m%d-%H%M%S-") output_dir = '../quora-output/' + strftime("%m%d") mkdir(output_dir) # EMBEDDING_FILE = BASE_DIR + 'glove.840B.quoraVocab.300d_sample.txt' # TRAIN_DATA_FILE = BASE_DIR + 'train_sample.csv' # TEST_DATA_FILE = BASE_DIR + 'test_sample.csv' # EPOCHES = 2 # train_feature_path = BASE_DIR + 'train_extra_features_sample.csv' # test_feature_path = BASE_DIR + 'test_extra_features_sample.csv' # train_bowl_feature_path = BASE_DIR + 'train_features_1bowl_sample.csv' # test_bowl_feature_path = BASE_DIR + 'test_features_1bowl_sample.csv' feature_list = "minkowski_distance,skew_q1vec,skew_q2vec, euclidean_distance,braycurtis_distance," + \ " norm_wmd, tfidf_wm,tfidf_wm_stops,cosine_distance, wmd,cityblock_distance, " + \ "kur_q1vec,kur_q2vec, canberra_distance, char_ratio, " + \ "wc_diff,wc_diff_unique,wc_diff_unq_stop,char_diff,char_diff_unq_stop,total_unique_words," + \ "total_unq_words_stop,len_q1,len_q2,len_char_q1,len_char_q2,len_word_q1,len_word_q2," + \
def train(args): timestr = time.strftime("%Y%m%d-%H%M%S-") output_dir = args.out_dir_path + '/' + time.strftime("%m%d") mkdir(output_dir) setLogger(timestr, out_dir=output_dir) print_args(args) if args.load_input_pkl == '': # process train and test data logger.info('Loading training file...') _, train_question1, train_question2, train_y = get_pdTable( args.train_path) # train_question1, train_question2, train_y = csv_processing(args.train_path) logger.info('Train csv: %d line loaded ' % len(train_question1)) logger.info('Loading test file...') if args.predict_test: test_ids, test_question1, test_question2 = get_pdTable( args.test_path, notag=True) else: test_ids, test_question1, test_question2, test_y = get_pdTable( args.test_path) # test_question1, test_question2, test_ids = csv_processing(args.test_path, test=True) logger.info('Test csv: %d line loaded ' % len(test_question1)) logger.info('Text cleaning... ') train_question1, train_maxLen1 = text_cleaner(train_question1) train_question2, train_maxLen2 = text_cleaner(train_question2) test_question1, test_maxLen1 = text_cleaner(test_question1) test_question2, test_maxLen2 = text_cleaner(test_question2) # train_question1, train_maxLen1 = tokenizeIt(train_question1, clean=args.rawMaterial) # train_question2, train_maxLen2 = tokenizeIt(train_question2, clean=args.rawMaterial) # test_question1, test_maxLen1 = tokenizeIt(test_question1, clean=args.rawMaterial) # test_question2, test_maxLen2 = tokenizeIt(test_question2, clean=args.rawMaterial) inputLength = max(train_maxLen1, train_maxLen2, test_maxLen1, test_maxLen2) logger.info('Max input length: %d ' % inputLength) inputLength = 30 logger.info('Reset max length to 30') tokenizer = Tokenizer(num_words=MAX_NB_WORDS) tokenizer.fit_on_texts(train_question1 + train_question2 + test_question1 + test_question2) sequences_1 = tokenizer.texts_to_sequences(train_question1) sequences_2 = tokenizer.texts_to_sequences(train_question2) test_sequences_1 = tokenizer.texts_to_sequences(test_question1) test_sequences_2 = tokenizer.texts_to_sequences(test_question2) del train_question1, train_question2, test_question1, test_question2 word_index = tokenizer.word_index logger.info('Found %s unique tokens' % len(word_index)) train_x1 = pad_sequences(sequences_1, maxlen=inputLength) train_x2 = pad_sequences(sequences_2, maxlen=inputLength) train_y = array(train_y) logger.info('Shape of data tensor: (%d, %d)' % train_x1.shape) logger.info('Shape of label tensor: (%d, )' % train_y.shape) test_x1 = pad_sequences(test_sequences_1, maxlen=inputLength) test_x2 = pad_sequences(test_sequences_2, maxlen=inputLength) test_ids = array(test_ids) if not args.predict_test: test_y = array(test_y) del sequences_1, sequences_2, test_sequences_1, test_sequences_2 if args.save_model: with open(output_dir + '/' + timestr + 'input_train_test.pkl', 'wb') as input_file: logger.info('Dumping processed input to pickle...') pkl.dump((train_x1, train_x2, train_y, test_x1, test_x2, test_ids, tokenizer), input_file) else: with open(args.load_input_pkl, 'rb') as input_file: train_x1, train_x2, train_y, test_x1, test_x2, test_ids, tokenizer = pkl.load( input_file) logger.info('Shape of data tensor: (%d, %d)' % train_x1.shape) logger.info('Shape of label tensor: (%d, )' % train_y.shape) word_index = tokenizer.word_index inputLength = 30 logger.info('Reset max length to 30') if args.w2v or args.ft_src: if args.w2v.endswith('.pkl'): with open(args.w2v, 'rb') as embd_file: logger.info('Loading word embedding from pickle...') embdw2v, vocabReverseDict = pkl.load(embd_file) if not len(vocabReverseDict) == len(word_index): logger.info('WARNING: reversed dict len incorrect %d , but word dict len %d ' % \ (len(vocabReverseDict), len(word_index))) else: logger.info('Loading word embedding from text file...') embdw2v, vocabReverseDict = embdReader( args.w2v, args.embd_dim, word_index, MAX_NB_WORDS, fasttext_source=args.ft_src, ft_dim=args.ft_dim, skip_header=args.skip_header, initializer=args.embd_init) if args.save_model: with open( output_dir + '/' + timestr + 'embd_dump.' + str(args.embd_dim + args.ft_dim) + 'd.pkl', 'wb') as embd_file: logger.info('Dumping word embedding to pickle...') pkl.dump((embdw2v, vocabReverseDict), embd_file) else: embdw2v = None # if args.load_vocab_from_file: # with open(args.load_vocab_from_file, 'rb') as vocab_file: # (vocabDict, vocabReverseDict) = pkl.load(vocab_file) # unk = None # if args.w2v: # if args.w2v.endswith('.pkl'): # with open(args.w2v, 'rb') as embd_file: # embdw2v = pkl.load(embd_file) # else: # from util.data_processing import w2vEmbdReader # embdw2v = w2vEmbdReader(args.w2v, vocabReverseDict, args.embd_dim) # with open(output_dir + '/'+ timestr + 'embd_dump.' + str(args.embd_dim) + 'd.pkl', 'wb') as embd_file: # pkl.dump(embdw2v, embd_file) # else: # embdw2v = None # else: # from util.data_processing import createVocab # vocabDict, vocabReverseDict = createVocab([train_question1, train_question2, test_question1, test_question2], # min_count=3, reservedList=['<pad>', '<unk>']) # embdw2v = None # unk = '<unk>' ## logger.info(vocabDict) # # word to padded numerical np array # from util.data_processing import word2num # train_x1 = word2num(train_question1, vocabDict, unk, inputLength, padding='pre') # train_x2 = word2num(train_question2, vocabDict, unk, inputLength, padding='pre') # test_x1 = word2num(test_question1, vocabDict, unk, inputLength, padding='pre') # test_x2 = word2num(test_question2, vocabDict, unk, inputLength, padding='pre') # Loading train features if not args.train_feature_path == '': logger.info('Loading train features from file %s ' % args.train_feature_path) df_train = read_csv(args.train_feature_path, encoding="ISO-8859-1") if not args.feature_list == '': feature_list = args.feature_list.split(',') train_features = DataFrame() for feature_name in feature_list: train_features[feature_name.strip()] = df_train[ feature_name.strip()] elif args.fidx_end == 0: train_features = df_train.iloc[:, args.fidx_start:] else: train_features = df_train.iloc[:, args.fidx_start:args.fidx_end] if not args.train_bowl_feature_path == '': logger.info('Loading train 1bowl features from file %s ' % args.train_bowl_feature_path) df_train = read_csv(args.train_bowl_feature_path, encoding="ISO-8859-1") if not args.bowl_feat_list == '': bowl_feat_list = args.bowl_feat_list.split(',') for feature_name in bowl_feat_list: train_features[feature_name.strip()] = df_train[ feature_name.strip()] else: for feature_name in df_train.columns: if feature_name.startswith('z_'): train_features[feature_name] = df_train[feature_name] logger.info('Final train feature list: \n %s ' % ','.join(list(train_features.columns.values))) feature_length = len(train_features.columns) train_features = train_features.replace([inf, -inf, nan], 0) train_features = array(train_features) logger.info('Loaded train feature shape: (%d, %d) ' % train_features.shape) del df_train # Loading test features logger.info('Loading test features from file %s ' % args.test_feature_path) df_test = read_csv(args.test_feature_path, encoding="ISO-8859-1") if not args.feature_list == '': feature_list = args.feature_list.split(',') test_features = DataFrame() for feature_name in feature_list: test_features[feature_name.strip()] = df_test[ feature_name.strip()] elif args.fidx_end == 0: test_features = df_test.iloc[:, args.fidx_start:] else: test_features = df_test.iloc[:, args.fidx_start:args.fidx_end] if not args.test_bowl_feature_path == '': logger.info('Loading test 1bowl features from file %s ' % args.test_bowl_feature_path) df_test = read_csv(args.test_bowl_feature_path, encoding="ISO-8859-1") if not args.bowl_feat_list == '': bowl_feat_list = args.bowl_feat_list.split(',') for feature_name in bowl_feat_list: test_features[feature_name.strip()] = df_test[ feature_name.strip()] else: for feature_name in df_test.columns: if feature_name.startswith('z_'): test_features[feature_name] = df_test[feature_name] test_features = test_features.replace([inf, -inf, nan], 0) test_features = array(test_features) logger.info('Loaded test feature shape: (%d, %d) ' % test_features.shape) del df_test # Normalize Data ss = StandardScaler() ss.fit(vstack((train_features, test_features))) train_features = ss.transform(train_features) test_features = ss.transform(test_features) del ss logger.info('Features normalized ') train_x1_aug = vstack((train_x1, train_x2)) train_x2_aug = vstack((train_x2, train_x1)) train_y = concatenate((train_y, train_y)) train_x = [train_x1_aug, train_x2_aug] test_x = [test_x1, test_x2] if not args.train_feature_path == '': train_features = vstack((train_features, train_features)) train_x += [train_features] test_x += [test_features] # ######################################## # ## sample train/validation data # ######################################## # # np.random.seed(1234) # perm = random.permutation(len(train_x1)) # idx_train = perm[:int(len(train_x1) * (1 - args.valid_split))] # idx_val = perm[int(len(train_x1) * (1 - args.valid_split)):] # # data_1_train = vstack((train_x1[idx_train], train_x2[idx_train])) # data_2_train = vstack((train_x2[idx_train], train_x1[idx_train])) # leaks_train = vstack((train_features[idx_train], train_features[idx_train])) # labels_train = concatenate((train_y[idx_train], train_y[idx_train])) # # data_1_val = vstack((train_x1[idx_val], train_x2[idx_val])) # data_2_val = vstack((train_x2[idx_val], train_x1[idx_val])) # leaks_val = vstack((train_features[idx_val], train_features[idx_val])) # labels_val = concatenate((train_y[idx_val], train_y[idx_val])) # re_weight = True # whether to re-weight classes to fit the 17.5% share in test set # weight_val = ones(len(labels_val)) # if re_weight: # weight_val *= 0.472001959 # weight_val[labels_val == 0] = 1.309028344 ######################################## ## add class weight ######################################## if args.re_weight: class_weight = {0: 1.309028344, 1: 0.472001959} else: class_weight = None # # Dump vocab # if not args.load_vocab_from_file: # with open(output_dir + '/'+ timestr + 'vocab.pkl', 'wb') as vocab_file: # pkl.dump((vocabDict, vocabReverseDict), vocab_file) if args.load_model_json: with open(args.load_model_json, 'r') as json_file: rnnmodel = model_from_json(json_file.read(), custom_objects={ "DenseWithMasking": DenseWithMasking, "Conv1DWithMasking": Conv1DWithMasking, "MaxOverTime": MaxOverTime, "MeanOverTime": MeanOverTime }) logger.info('Loaded model from saved json') else: if args.train_feature_path == '': rnnmodel = getModel(args, inputLength, len(word_index) + 1, embd=embdw2v) else: rnnmodel = getModel(args, inputLength, len(word_index) + 1, embd=embdw2v, feature_length=feature_length) if args.load_model_weights: rnnmodel.load_weights(args.load_model_weights) logger.info('Loaded model from saved weights') if args.optimizer == 'rmsprop': optimizer = RMSprop(lr=args.learning_rate) else: optimizer = args.optimizer myMetrics = 'acc' # 'binary_accuracy' # 'mse' rnnmodel.compile(loss=args.loss, optimizer=optimizer, metrics=[myMetrics]) rnnmodel.summary() ## Plotting model logger.info('Plotting model architecture') plot_model(rnnmodel, to_file=output_dir + '/' + timestr + 'model_plot.png') logger.info(' Done') if args.save_model: ## Save model architecture logger.info('Saving model architecture') with open(output_dir + '/' + timestr + 'model_config.json', 'w') as arch: arch.write(rnnmodel.to_json(indent=2)) logger.info(' Done') # train and test model myCallbacks = [] train_logger = TrainLogger() myCallbacks.append(train_logger) if args.eval_on_epoch: from util.model_eval import Evaluator evl = Evaluator(args, output_dir, timestr, myMetrics, test_x, test_y, vocabReverseDict) myCallbacks.append(evl) if args.save_model: bst_model_path = output_dir + '/' + timestr + 'best_model_weights.h5' model_checkpoint = ModelCheckpoint(bst_model_path, save_best_only=True, save_weights_only=True, verbose=1) myCallbacks.append(model_checkpoint) if args.plot: if not args.eval_on_epoch: plot_pic = PlotPic(args, output_dir, timestr, myMetrics) myCallbacks.append(plot_pic) if args.earlystop: earlystop = EarlyStopping(patience=args.earlystop, verbose=1, mode='auto') myCallbacks.append(earlystop) rnnmodel.fit(train_x, train_y, validation_split=args.valid_split, batch_size=args.train_batch_size, epochs=args.epochs, class_weight=class_weight, callbacks=myCallbacks) # rnnmodel.fit([data_1_train, data_2_train, leaks_train], labels_train, # validation_data=([data_1_val, data_2_val, leaks_val], labels_val, weight_val), # epochs=args.epochs, batch_size=args.train_batch_size, shuffle=True, # class_weight=class_weight, callbacks=myCallbacks) if args.predict_test: logger.info("Tuning model to best record...") rnnmodel.load_weights(bst_model_path) logger.info("Predicting test file result...") preds = rnnmodel.predict(test_x, batch_size=args.eval_batch_size, verbose=1) preds = squeeze(preds) logger.info('Write predictions into file... Total line: %d ' % len(preds)) with open(output_dir + '/' + timestr + 'predict.csv', 'w', encoding='utf8') as fwrt: writer_sub = csv.writer(fwrt) writer_sub.writerow(['test_id', 'is_duplicate']) idx = 0 for itm in tqdm(preds): writer_sub.writerow([idx, itm]) idx += 1 elif not args.eval_on_epoch: logger.info("Evaluating test set...") tloss, tacc = rnnmodel.evaluate(test_x, test_y, batch_size=args.eval_batch_size, verbose=1) logger.info("Test loss: %.4f Test Accuracy: %.2f%%" % (tloss, 100 * tacc))
datetime.now().strftime('%b%d_%H-%M-%S')) cfg.LOSS_TYPES = [arg_loss, 'CLS'] else: os.environ["CUDA_VISIBLE_DEVICES"] = cfg.GPU_IDS # Setting random seed if cfg.MANUAL_SEED is None: cfg.MANUAL_SEED = random.randint(1, 10000) random.seed(cfg.MANUAL_SEED) torch.manual_seed(cfg.MANUAL_SEED) torch.backends.cudnn.benchmark = True project_name = reduce(lambda x, y: str(x) + '/' + str(y), os.path.realpath(__file__).split(os.sep)[:-1]) util.mkdir('logs') # resize_size = 256 # crop_size = 224 # image_h,image_w=416,544 train_transforms = list() ms_targets = [] # train_transforms.append(segmentation_dataset.Resize(cfg.LOAD_SIZE_cityscapes)) train_transforms.append(segmentation_dataset.RandomScale( cfg.RANDOM_SCALE_SIZE)) # train_transforms.append(segmentation_dataset.RandomRotate()) # # train_transforms.append(segmentation_dataset.RandomGaussianBlur()) train_transforms.append( segmentation_dataset.ColorJitter(brightness=0.5, contrast=0.5,
def main(): cfg = DefaultConfig() args = { 'seg_resnet_sunrgbd': SEG_RESNET_SUNRGBD_CONFIG().args(), 'seg_resnet_cityscapes': SEG_RESNET_CITYSCAPE_CONFIG().args(), 'rec_resnet_sunrgbd': REC_RESNET_SUNRGBD_CONFIG().args(), 'rec_resnet_nyud2': REC_RESNET_NYUD2_CONFIG().args(), 'rec_resnet_mit67': REC_RESNET_MIT67_CONFIG().args(), 'infomax_resnet_sunrgbd': INFOMAX_RESNET_SUNRGBD_CONFIG().args(), 'infomax_resnet_nyud2': INFOMAX_RESNET_NYUD2_CONFIG().args() } # use shell if len(sys.argv) > 1: device_ids = torch.cuda.device_count() print('device_ids:', device_ids) gpu_ids, config_key = sys.argv[1:] cfg.parse(args[config_key]) cfg.GPU_IDS = gpu_ids.split(',') else: # seg_resnet_sunrgbd # seg_resnet_cityscapes # infomax_resnet_sunrgbd # rec_resnet_sunrgbd # rec_resnet_nyud2 # rec_resnet_mit67 # infomax_resnet_nyud2 config_key = 'rec_resnet_sunrgbd' cfg.parse(args[config_key]) os.environ['CUDA_VISIBLE_DEVICES'] = ','.join( map(lambda x: str(x), cfg.GPU_IDS)) trans_task = '' + cfg.WHICH_DIRECTION if not cfg.NO_TRANS: if cfg.MULTI_MODAL: trans_task = trans_task + '_multimodal_' if 'SEMANTIC' in cfg.LOSS_TYPES or 'PIX2PIX' in cfg.LOSS_TYPES: trans_task = trans_task + '_alpha_' + str(cfg.ALPHA_CONTENT) evaluate_type = 'sliding_window' if cfg.SLIDE_WINDOWS else 'center_crop' log_name = ''.join( [cfg.TASK, '_', cfg.ARCH, '_', trans_task, '_', cfg.DATASET]) cfg.LOG_NAME = ''.join([ log_name, '_', '.'.join(cfg.LOSS_TYPES), '_', evaluate_type, '_gpus_', str(len(cfg.GPU_IDS)), '_', datetime.now().strftime('%b%d_%H-%M-%S') ]) cfg.LOG_PATH = os.path.join(cfg.LOG_PATH, cfg.MODEL, cfg.LOG_NAME) # Setting random seed if cfg.MANUAL_SEED is None: cfg.MANUAL_SEED = random.randint(1, 10000) random.seed(cfg.MANUAL_SEED) torch.manual_seed(cfg.MANUAL_SEED) torch.backends.cudnn.benchmark = True # cudnn.deterministic = True project_name = reduce(lambda x, y: str(x) + '/' + str(y), os.path.realpath(__file__).split(os.sep)[:-1]) print('>>> task path is {0}'.format(project_name)) util.mkdir('logs') # dataset = segmentation_dataset_cv2 train_transforms = list() val_transforms = list() ms_targets = [] train_transforms.append(dataset.Resize(cfg.LOAD_SIZE)) # train_transforms.append(dataset.RandomScale(cfg.RANDOM_SCALE_SIZE)) # # train_transforms.append(dataset.RandomRotate()) # train_transforms.append(dataset.RandomCrop_Unaligned(cfg.FINE_SIZE, pad_if_needed=True, fill=0)) # train_transforms.append( dataset.RandomCrop(cfg.FINE_SIZE, pad_if_needed=True, fill=0)) # train_transforms.append(dataset.RandomHorizontalFlip()) if cfg.TARGET_MODAL == 'lab': train_transforms.append(dataset.RGB2Lab()) if cfg.MULTI_SCALE: for item in cfg.MULTI_TARGETS: ms_targets.append(item) train_transforms.append( dataset.MultiScale(size=cfg.FINE_SIZE, scale_times=cfg.MULTI_SCALE_NUM, ms_targets=ms_targets)) train_transforms.append(dataset.ToTensor(ms_targets=ms_targets)) train_transforms.append( dataset.Normalize(mean=cfg.MEAN, std=cfg.STD, ms_targets=ms_targets)) val_transforms.append(dataset.Resize(cfg.LOAD_SIZE)) if not cfg.SLIDE_WINDOWS: val_transforms.append(dataset.CenterCrop((cfg.FINE_SIZE))) if cfg.MULTI_SCALE: val_transforms.append( dataset.MultiScale(size=cfg.FINE_SIZE, scale_times=cfg.MULTI_SCALE_NUM, ms_targets=ms_targets)) val_transforms.append(dataset.ToTensor(ms_targets=ms_targets)) val_transforms.append( dataset.Normalize(mean=cfg.MEAN, std=cfg.STD, ms_targets=ms_targets)) train_dataset = dataset.__dict__[cfg.DATASET]( cfg=cfg, transform=transforms.Compose(train_transforms), data_dir=cfg.DATA_DIR_TRAIN, phase_train=True) val_dataset = dataset.__dict__[cfg.DATASET]( cfg=cfg, transform=transforms.Compose(val_transforms), data_dir=cfg.DATA_DIR_VAL, phase_train=False) cfg.CLASS_WEIGHTS_TRAIN = train_dataset.class_weights cfg.IGNORE_LABEL = train_dataset.ignore_label cfg.train_dataset = train_dataset cfg.val_dataset = val_dataset port = random.randint(8001, 9000) ngpus_per_node = len(cfg.GPU_IDS) if cfg.MULTIPROCESSING_DISTRIBUTED: cfg.rank = 0 cfg.ngpus_per_node = ngpus_per_node cfg.dist_url = 'tcp://127.0.0.1:' + str(port) cfg.dist_backend = 'nccl' cfg.opt_level = 'O0' cfg.world_size = 1 cfg.print_args() if cfg.MULTIPROCESSING_DISTRIBUTED: cfg.world_size = cfg.ngpus_per_node * cfg.world_size mp.spawn(main_worker, nprocs=cfg.ngpus_per_node, args=(cfg.ngpus_per_node, cfg)) else: # Simply call main_worker function main_worker(cfg.GPU_IDS, ngpus_per_node, cfg)
def inference(args): timestr = time.strftime("%Y%m%d-%H%M%S-") output_dir = args.out_dir_path + '/' + time.strftime("%m%d") mkdir(output_dir) setLogger(timestr, out_dir=output_dir) print_args(args) if args.load_input_pkl == '': raise NotImplementedError( 'only support loading testing materials from pickle') else: with open(args.load_input_pkl, 'rb') as input_file: train_x1, train_x2, train_y, test_x1, test_x2, test_ids, tokenizer = pkl.load( input_file) logger.info('Shape of test data tensor: (%d, %d)' % test_x1.shape) word_index = tokenizer.word_index logger.info('Loaded %s unique tokens' % len(word_index)) if not args.test_path == '': if args.predict_test: test_ids, test_question1, test_question2 = get_pdTable( args.test_path, notag=True) else: test_ids, test_question1, test_question2, test_y = get_pdTable( args.test_path) test_question1, test_maxLen1 = text_cleaner(test_question1) test_question2, test_maxLen2 = text_cleaner(test_question2) inputLength = max(test_maxLen1, test_maxLen2) logger.info('Max input length: %d ' % inputLength) inputLength = 30 logger.info('Reset max length to 30') test_sequences_1 = tokenizer.texts_to_sequences(test_question1) test_sequences_2 = tokenizer.texts_to_sequences(test_question2) test_x1 = pad_sequences(test_sequences_1, maxlen=inputLength) test_x2 = pad_sequences(test_sequences_2, maxlen=inputLength) test_ids = array(test_ids) if not args.predict_test: test_y = array(test_y) # Loading train features if not args.train_feature_path == '': logger.info('Loading train features from file %s ' % args.train_feature_path) df_train = read_csv(args.train_feature_path, encoding="ISO-8859-1") if not args.feature_list == '': feature_list = args.feature_list.split(',') train_features = DataFrame() for feature_name in feature_list: train_features[feature_name.strip()] = df_train[ feature_name.strip()] elif args.fidx_end == 0: train_features = df_train.iloc[:, args.fidx_start:] else: train_features = df_train.iloc[:, args.fidx_start:args.fidx_end] if not args.train_bowl_feature_path == '': logger.info('Loading train 1bowl features from file %s ' % args.train_bowl_feature_path) df_train = read_csv(args.train_bowl_feature_path, encoding="ISO-8859-1") if not args.bowl_feat_list == '': bowl_feat_list = args.bowl_feat_list.split(',') for feature_name in bowl_feat_list: train_features[feature_name.strip()] = df_train[ feature_name.strip()] else: for feature_name in df_train.columns: if feature_name.startswith('z_'): train_features[feature_name] = df_train[feature_name] logger.info('Final train feature list: \n %s ' % ','.join(list(train_features.columns.values))) feature_length = len(train_features.columns) train_features = train_features.replace([inf, -inf, nan], 0) train_features = array(train_features) logger.info('Loaded train feature shape: (%d, %d) ' % train_features.shape) del df_train # Loading test features logger.info('Loading test features from file %s ' % args.test_feature_path) df_test = read_csv(args.test_feature_path, encoding="ISO-8859-1") if not args.feature_list == '': feature_list = args.feature_list.split(',') test_features = DataFrame() for feature_name in feature_list: test_features[feature_name.strip()] = df_test[ feature_name.strip()] elif args.fidx_end == 0: test_features = df_test.iloc[:, args.fidx_start:] else: test_features = df_test.iloc[:, args.fidx_start:args.fidx_end] if not args.test_bowl_feature_path == '': logger.info('Loading test 1bowl features from file %s ' % args.test_bowl_feature_path) df_test = read_csv(args.test_bowl_feature_path, encoding="ISO-8859-1") if not args.bowl_feat_list == '': bowl_feat_list = args.bowl_feat_list.split(',') for feature_name in bowl_feat_list: test_features[feature_name.strip()] = df_test[ feature_name.strip()] else: for feature_name in df_test.columns: if feature_name.startswith('z_'): test_features[feature_name] = df_test[feature_name] test_features = test_features.replace([inf, -inf, nan], 0) test_features = array(test_features) logger.info('Loaded test feature shape: (%d, %d) ' % test_features.shape) del df_test # Normalize Data ss = StandardScaler() ss.fit(vstack((train_features, test_features))) # train_features = ss.transform(train_features) test_features = ss.transform(test_features) del ss logger.info('Test Features normalized ') test_x = [test_x1, test_x2] if not args.test_feature_path == '': test_x += [test_features] if args.load_model_json: with open(args.load_model_json, 'r') as json_file: rnnmodel = model_from_json(json_file.read(), custom_objects={ "DenseWithMasking": DenseWithMasking, "Conv1DWithMasking": Conv1DWithMasking, "MaxOverTime": MaxOverTime, "MeanOverTime": MeanOverTime }) logger.info('Loaded model from saved json') if args.load_model_weights: logger.info('Loading model from saved weights') rnnmodel.load_weights(args.load_model_weights) if args.predict_test: logger.info("Predicting test file result...") preds = rnnmodel.predict(test_x, batch_size=args.eval_batch_size, verbose=1) preds = squeeze(preds) logger.info('Write predictions into file... Total line: %d ' % len(preds)) with open(output_dir + '/' + timestr + 'predict.csv', 'w', encoding='utf8') as fwrt: writer_sub = csv.writer(fwrt) writer_sub.writerow(['test_id', 'is_duplicate']) idx = 0 for itm in tqdm(preds): writer_sub.writerow([idx, itm]) idx += 1 logger.info('Predicted results written to file: %s' % (output_dir + '/' + timestr + 'predict.csv')) else: if args.optimizer == 'rmsprop': optimizer = RMSprop(lr=args.learning_rate) else: optimizer = args.optimizer myMetrics = 'acc' # 'binary_accuracy' # 'mse' rnnmodel.compile(loss=args.loss, optimizer=optimizer, metrics=[myMetrics]) rnnmodel.summary() logger.info("Evaluating test set...") tloss, tacc = rnnmodel.evaluate(test_x, test_y, batch_size=args.eval_batch_size, verbose=1) logger.info("Test loss: %.4f Test Accuracy: %.2f%%" % (tloss, 100 * tacc))
def train(): cfg = DefaultConfig() args = { 'resnet18': RESNET18_SUNRGBD_CONFIG().args(), } # Setting random seed if cfg.MANUAL_SEED is None: cfg.MANUAL_SEED = random.randint(1, 10000) random.seed(cfg.MANUAL_SEED) torch.manual_seed(cfg.MANUAL_SEED) # args for different backbones cfg.parse(args['resnet18']) os.environ["CUDA_VISIBLE_DEVICES"] = cfg.GPU_IDS device_ids = torch.cuda.device_count() print('device_ids:', device_ids) project_name = reduce(lambda x, y: str(x) + '/' + str(y), os.path.realpath(__file__).split(os.sep)[:-1]) util.mkdir('logs') # data train_dataset = dataset.AlignedConcDataset( cfg, data_dir=cfg.DATA_DIR_TRAIN, transform=transforms.Compose([ dataset.Resize((cfg.LOAD_SIZE, cfg.LOAD_SIZE)), dataset.RandomCrop((cfg.FINE_SIZE, cfg.FINE_SIZE)), dataset.RandomHorizontalFlip(), dataset.ToTensor(), dataset.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ])) val_dataset = dataset.AlignedConcDataset( cfg, data_dir=cfg.DATA_DIR_VAL, transform=transforms.Compose([ dataset.Resize((cfg.LOAD_SIZE, cfg.LOAD_SIZE)), dataset.CenterCrop((cfg.FINE_SIZE, cfg.FINE_SIZE)), dataset.ToTensor(), dataset.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ])) batch_size_val = cfg.BATCH_SIZE unlabeled_loader = None if cfg.UNLABELED: unlabeled_dataset = dataset.AlignedConcDataset( cfg, data_dir=cfg.DATA_DIR_UNLABELED, transform=transforms.Compose([ dataset.Resize((cfg.LOAD_SIZE, cfg.LOAD_SIZE)), dataset.RandomCrop((cfg.FINE_SIZE, cfg.FINE_SIZE)), dataset.RandomHorizontalFlip(), dataset.ToTensor(), dataset.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ]), labeled=False) unlabeled_loader = DataProvider(cfg, dataset=unlabeled_dataset) train_loader = DataProvider(cfg, dataset=train_dataset, batch_size=batch_size_val) val_loader = DataProvider(cfg, dataset=val_dataset, batch_size=batch_size_val, shuffle=False) # class weights num_classes_train = list( Counter([i[1] for i in train_loader.dataset.imgs]).values()) cfg.CLASS_WEIGHTS_TRAIN = torch.FloatTensor(num_classes_train) writer = SummaryWriter(log_dir=cfg.LOG_PATH) # tensorboard model = TRecgNet(cfg, writer=writer) model.set_data_loader(train_loader, val_loader, unlabeled_loader) if cfg.RESUME: checkpoint_path = os.path.join(cfg.CHECKPOINTS_DIR, cfg.RESUME_PATH) checkpoint = torch.load(checkpoint_path) load_epoch = checkpoint['epoch'] model.load_checkpoint(model.net, checkpoint_path, checkpoint, data_para=True) cfg.START_EPOCH = load_epoch if cfg.INIT_EPOCH: # just load pretrained parameters print('load checkpoint from another source') cfg.START_EPOCH = 1 print('>>> task path is {0}'.format(project_name)) # train model.train_parameters(cfg) print('save model ...') model_filename = '{0}_{1}_{2}.pth'.format(cfg.MODEL, cfg.WHICH_DIRECTION, cfg.NITER_TOTAL) model.save_checkpoint(cfg.NITER_TOTAL, model_filename) if writer is not None: writer.close()
def train(args): timestr = time.strftime("%Y%m%d-%H%M%S-") output_dir = args.out_dir_path + '/' + time.strftime("%m%d") mkdir(output_dir) setLogger(timestr, out_dir=output_dir) # process train and test data _, train_title, train_content, train_tag = get_pdTable(args.train_path) _, test_title, test_content, test_tag = get_pdTable(args.test_path) train_body = tableMerge([train_title, train_content]) test_body = tableMerge([test_title, test_content]) train_body, _ = tokenizeIt(train_body, clean=True) test_body, _ = tokenizeIt(test_body, clean=True) train_tag, _ = tokenizeIt(train_tag) test_tag, _ = tokenizeIt(test_tag) # inputLength = max(train_maxLen, test_maxLen) inputLength = 400 outputLength = 5 + 1 if args.w2v: embdw2v, vocabDict, vocabReverseDict = makeEmbedding(args, [train_body, test_body]) unk = None eof = None else: vocabDict, vocabReverseDict = createVocab([train_body, test_body], min_count=3, reservedList=['<pad>', '<EOF>', '<unk>']) embdw2v = None unk = '<unk>' eof = '<EOF>' pred_vocabDict, pred_vocabReverseDict = createVocab([train_tag,], min_count=3, reservedList=['<pad>', '<EOF>', '<unk>']) # logger.info(vocabDict) logger.info(pred_vocabReverseDict) # word to padded numerical np array train_x = word2num(train_body, vocabDict, unk, inputLength, padding='pre',eof=eof) test_x = word2num(test_body, vocabDict, unk, inputLength, padding='pre', eof=eof) train_y = word2num(train_tag, pred_vocabDict, unk, outputLength, padding='post', eof=eof) train_y = to_categorical2D(train_y, len(pred_vocabDict)) test_y = word2num(test_tag, pred_vocabDict, unk, outputLength, padding='post', eof=eof) test_y = to_categorical2D(test_y, len(pred_vocabDict)) # create model rnnmodel = getModel(args, inputLength, outputLength, len(vocabDict), len(pred_vocabDict), embd=embdw2v) if args.optimizer == 'rmsprop': from keras.optimizers import RMSprop optimizer = RMSprop(lr=args.learning_rate) else: optimizer = args.optimizer if args.loss == 'my_binary_crossentropy': from util.my_optimizer import my_binary_crossentropy loss = my_binary_crossentropy else: loss = args.loss myMetrics = 'fmeasure' rnnmodel.compile(loss=loss, optimizer=optimizer, metrics=[myMetrics]) rnnmodel.summary() if args.save_model: ## Plotting model logger.info('Plotting model architecture') from keras.utils.visualize_util import plot plot(rnnmodel, to_file = output_dir + '/' + timestr + 'model_plot.png') logger.info(' Done') ## Save model architecture logger.info('Saving model architecture') with open(output_dir + '/'+ timestr + 'model_config.json', 'w') as arch: arch.write(rnnmodel.to_json(indent=2)) logger.info(' Done') # train and test model myCallbacks = [] if args.eval_on_epoch: evl = Evaluator(args, output_dir, timestr, myMetrics, test_x, test_y, vocabReverseDict, pred_vocabReverseDict, use_argm=True) myCallbacks.append(evl) if args.earlystop: earlystop = EarlyStopping(patience = args.earlystop, verbose=1, mode='auto') myCallbacks.append(earlystop) rnnmodel.fit(train_x, train_y, validation_split=args.valid_split, batch_size=args.train_batch_size, nb_epoch=args.epochs, callbacks=myCallbacks) if not args.eval_on_epoch: rnnmodel.evaluate(test_x, test_y, batch_size=args.eval_batch_size) # test output (remove duplicate, remove <pad> <unk>, comparable layout, into csv) # final inference: output(remove duplicate, remove <pad> <unk>, limit output words to 3 or 2 or 1..., into csv)
default=None, type=int, help='number of trainable layers of backbone') parser.add_argument( "--test-only", dest="test_only", help="Only test the model", action="store_true", ) parser.add_argument( "--pretrained", dest="pretrained", help="Use pre-trained models from the modelzoo", action="store_true", ) # distributed training parameters parser.add_argument('--world-size', default=1, type=int, help='number of distributed processes') parser.add_argument('--dist-url', default='env://', help='url used to set up distributed training') args = parser.parse_args() if args.output_dir: utils.mkdir(args.output_dir) main(args)