def tower_loss(scope): reader = read.Reader(path=FLAGS.buckets + 'wavFile_train_frame_60.tfr', batch_size=FLAGS.batch_size, window_size=FLAGS.frequency // FLAGS.frame_count, kwidth=FLAGS.kwidth) logits = inference.Inference(reader.wav_raw, FLAGS.kwidth, 2, FLAGS.isTrain, scope=scope).build_model() loss.loss(logits=logits, labels=reader.label) losses = tf.get_collection('losses', scope) total_loss = tf.add_n(losses, name='total_loss') tf.add_to_collection('summary', tf.summary.scalar(scope + 'loss', losses[0])) return total_loss
def loadImages(self,iD,cont = None,downsampling=1,isotrope= False,pad = False): ''' iD is the the part of the data: train, validation or test cont is the number of images to load ''' if iD == 'train': Im_path = self.train_Im_path Seg_path = self.train_Seg_path elif iD == 'val': Im_path = self.validation_Im_path Seg_path = self.validation_Seg_path elif iD == 'test': Im_path = self.test_Im_path Seg_path = self.test_Seg_path imgs = glob.glob('../../Data/'+Im_path+'/*') segs = glob.glob('../../Data/'+Seg_path+'/*') imgs = np.sort(imgs) segs = np.sort(segs) x =len(imgs) images = [] segmentations= [] if cont == None: cont = x for i in range(cont): print 'ID: ',iD,'---- imgs ',imgs[i],' ---- seg',segs[i] images.append(read.Reader(isotrope)(imgs[i],downsampling,preprocessing = True,pad = pad)) segmentations.append(read.Reader(isotrope)(segs[i],downsampling,pad = pad)) images = np.asarray(images,dtype='float16') segmentations = np.asarray(segmentations,dtype='float16') print 'images loaded with shape: ',images.shape return images,segmentations
def process_dataset(dataset_fold, dataset_raw_dirpath, downsampling_factors, disp_max_abs_value): print("Processing images from {}".format(dataset_raw_dirpath)) # Create shard writers shard_writers = {} for downsampling_factor in downsampling_factors: filename_format = os.path.join( config.TFRECORDS_DIR, config.TFRECORD_FILENAME_FORMAT.format(dataset_fold, downsampling_factor)) shard_writer = dataset_utils.TFRecordShardWriter( filename_format, config.RECORDS_PER_SHARD) shard_writers[downsampling_factor] = shard_writer # Create reader reader = read.Reader(dataset_raw_dirpath, dataset_fold) # Create DispFieldMapsPatchCreator disp_field_maps_patch_creator = math_utils.DispFieldMapsPatchCreator( config.DISP_GLOBAL_SHAPE, config.DISP_PATCH_RES, config.DISP_MAP_COUNT, config.DISP_MODES, config.DISP_GAUSS_MU_RANGE, config.DISP_GAUSS_SIG_SCALING) for image_index, image_id in enumerate(reader.image_id_list): if (image_index + 1) % 10 == 0: print("Processing progression: {}/{}".format( image_index + 1, len(reader.image_id_list))) include_polygons = (dataset_fold == "val" or dataset_fold == "test") process_image(reader, image_id, downsampling_factors, disp_field_maps_patch_creator, disp_max_abs_value, include_polygons, shard_writers) # Close writers for downsampling_factor in downsampling_factors: shard_writers[downsampling_factor].close()
import converter import read # load files files = list(glob.glob(os.path.join("data", '*'))) correct_times = [ 228, 3026, 3665, 3309, 3191, 3618, 3446, 3821, 3634, 3070, 6398, 1492 ] t = tabel([ 'File name', 'Correct time', 'Calculated time', 'Is calculated time correct?' ]) index = 0 correct = 0 x = read.Reader() for file in files: x.read(file) converted_tasks = converter.converter(x.my_data) carlier_object = carlier_task.Carlier_Task(converted_tasks) c_max = carlier.do_carlier(carlier_object) t.add_row( [file[5:], correct_times[index], c_max, correct_times[index] == c_max]) if correct_times[index] == c_max: correct += 1 index += 1 string = str(str(correct) + "/" + str(index)) print(string) t.add_row(["", "", "", ""]) t.add_row(["", "", "Passed: ", string])
FLAGS = tf.flags.FLAGS print os.getcwd() tf.flags.DEFINE_string('test_file', 'wavFile_test_frame_60.tfr', '数据源地址') tf.flags.DEFINE_string('checkpointDir', 'saves/', "模型保存路径") tf.flags.DEFINE_string('summaryDir', 'logs', "tensorboard保存路径") tf.flags.DEFINE_integer('batch_size', 1, '批大小') tf.flags.DEFINE_integer('frame_count', 60, "帧数") tf.flags.DEFINE_integer('frequency', 16000, "采样率") tf.flags.DEFINE_integer('kwidth', 18, '窗格大小') tf.flags.DEFINE_integer('num_train', 1000, "训练次数") tf.flags.DEFINE_float('learning_rate', 3e-4, "学习速率") tf.flags.DEFINE_float('beta1', 0.5, "Adam动量") sess = tf.InteractiveSession() coord = tf.train.Coordinator() reader = read.Reader(path=FLAGS.test_file, batch_size=FLAGS.batch_size, window_size=FLAGS.frequency // FLAGS.frame_count, kwidth=FLAGS.kwidth) tf.train.start_queue_runners(sess=sess, coord=coord) logits = inference.Inference(reader.wav_raw, FLAGS.kwidth, 2, isTrain=False).build_model() loss_val = loss.loss(logits=logits, labels=reader.label) saver = tf.train.Saver() tf.global_variables_initializer().run() saver.restore(sess, os.path.join(FLAGS.checkpointDir)) tf.train.start_queue_runners(sess=sess, coord=coord) labels = tf.reshape(reader.label, [-1]) logits_predict, ground_truth = sess.run([logits, labels])
layer_prew = layer.shape layer = tf.reshape(layer, [-1, dim]) print "flat conv_layer: {} -> {}".format(layer_prew, layer.shape) with tf.variable_scope('full_connect'): for index, num_weight in enumerate(self.num_full_connect_weight): with tf.variable_scope('full_connect_{}'.format(index)): w = tf.get_variable('w', [layer.shape[-1], num_weight], initializer=self.w_init) b = tf.get_variable('b', [num_weight], initializer=self.b_init) layer_prew = layer.shape layer = tf.matmul(layer, w) + b print "full connect {} -> {}".format( layer_prew, layer.shape) return layer if __name__ == '__main__': import read sess = tf.InteractiveSession() reader = read.Reader('wavFile_train_frame_60.tfr', 1, 266, 32) tf.train.start_queue_runners() inference = Inference(reader.wav_raw, 32, 2) logits = inference.build_model() tf.global_variables_initializer().run() print reader.wav_raw