Exemplo n.º 1
0
def generate_data_extract_feature(corpus, annots, nlp):
    data = []
    possible_types = set()
    for id, sent in corpus.items():
        sent_split = nlp(sent)
        ents = list(filter(lambda tok: tok.ent_type_ != '', sent_split))
        for ent1, ent2 in combinations(ents, 2):
            feature_dict_12 = ey.extract_features(ent1, ent2, sent_split)
            feature_dict_21 = ey.extract_features(ent2, ent1, sent_split)
            feature_dict_12 = add_sent_data_to_dict_features(
                id, sent, ent1, ent2, feature_dict_12)
            feature_dict_21 = add_sent_data_to_dict_features(
                id, sent, ent2, ent1, feature_dict_21)
            # print(feature_dict_12)
            for annot in annots[id]:
                if annot[0] == ent1.text and annot[2] == ent2.text:
                    feature_dict_12['label'] = annot[1]
                    possible_types.add(feature_dict_12['concatenated-types'])
                    break
                if annot[0] == ent2.text and annot[2] == ent1.text:
                    feature_dict_21['label'] = annot[1]
                    possible_types.add(feature_dict_21['concatenated-types'])
                    break
            data.append(feature_dict_12)
            data.append(feature_dict_21)
    print('possible-ent-combinations:' + str(possible_types))
    return list(
        filter(
            lambda x: x['concatenated-types'] in possible_types or x[
                'concatenated-types'] in possible_train or x[
                    'concatenated-types'] in possible_dev, data))
Exemplo n.º 2
0
def binary_extract(frame_path,model_path,seq_path):
    files.make_dir(seq_path)
    paths=model_path if(type(model_path)==list) else files.top_files(model_path) 
    frame_dict=imgs.read_seqs(frame_path)
    for i,in_i in enumerate(paths):
        print(i)
        out_i= seq_path+'/'+in_i.split('/')[-1]
        extract.extract_features(frame_dict,in_i,out_i)
        gc.collect()
Exemplo n.º 3
0
def single_model(frame_path, dest_dir=None, n_epochs=200, nn_type="old"):
    if (not dest_dir):
        dest_dir = os.path.split(frame_path)[0]
    nn_path, seq_path, feat_path = dest_dir + '/nn', dest_dir + '/seq', dest_dir + '/feats'
    basic.simple_exp(frame_path,
                     nn_path,
                     n_epochs=n_epochs,
                     model_type=nn_type)
    extract.extract_features(frame_path, nn_path, seq_path)
    feats.compute_feats(seq_path, feat_path)
Exemplo n.º 4
0
def load_features(words, trainingDir) : 
    parentPath  = os.getcwd()
    features    = []
    features2   = []
    gestureArray = []
    os.chdir(trainingDir)                                                           # this changes the directory to the given path 

    # loop through the csv files, and store into 2D array
    for i in os.listdir(os.getcwd()):                                           
        if i in words:
            #print("Gesture i: ", i )
            os.chdir(parentPath + "/" + trainingDir + "/" + i)
            for dataFile in os.listdir(os.getcwd()):
                result              = extract.extract_features(dataFile)                # Extract features from the file _ note removed the time features 
                features.append(result)
                gestureArray.append(i)                                              # Keep track of the gesture labels


    # unroll loop of features2 -- test for everything and determine which combination of features is best to use for each file then make final decision 
    # Scaling the features 
    scaler          = StandardScaler().fit(features)
    features_scaled = scaler.transform(features) 
    os.chdir(parentPath)
    return features_scaled, features, gestureArray                                                             
Exemplo n.º 5
0
        return 'rock'


#Загружаем модель из файла
os.chdir('..\\Data\\model')
model = keras.models.load_model('model.h5')

os.chdir('..\\samples')
path = os.getcwd()

tracks = []
tracks_features = []

for filename in os.listdir(
        os.path.join(path)
):  #Заполняем список tracks данными, полученными из тестируемых треков
    songname = os.path.join(path, f'{filename}')
    features = extract_features(songname).split()
    tracks_features.append(np.array(features, dtype=float))
    tracks.append(filename)

scaler = StandardScaler()
tracks_features = scaler.fit_transform(np.array(
    tracks_features, dtype=float))  #Подготавливаем данные для модели

predictions = model.predict(tracks_features)  #Формирование предсказаний

for item in enumerate(predictions):  #Вывод предсказаний
    print(
        f'{tracks[item[0]]} - {decoding(np.argmax(item[1]))} : [classical - {int(item[1][0]*100)}% pop - {int(item[1][1]*100)}% rock - {int(item[1][2]*100)}%]'
    )
Exemplo n.º 6
0
for i in range(1, 21):
    header += f' mfcc{i}'
header += ' label'
header = header.split()  #Создание заголовка таблицы

os.chdir('..\\Data')
data_path = os.getcwd()  #Получаем доступ к нужной директории

file = open(os.path.join(data_path, 'data', 'data.csv'), 'w',
            newline='')  #Создание файла на запись данных
with file:
    writer = csv.writer(file)
    writer.writerow(header)
genres = 'classical pop rock'.split()
for g in genres:
    for filename in os.listdir(os.path.join(
            data_path,
            f'genres\\{g}')):  #Циклом проходим по всем трекам всех жанров
        songname = os.path.join(data_path, f'genres\\{g}\\{filename}')
        to_append = f'{filename} '
        to_append += extract_features(
            songname)  #Вычисляем свойства музыкального трека
        to_append += f' {g}'
        file = open(os.path.join(data_path, 'data', 'data.csv'),
                    'a',
                    newline='')
        with file:
            writer = csv.writer(file)
            writer.writerow(to_append.split())  #Записываем строку в таблицу
    print(f"Done for {g}")
# Save images to list
print("Reading in images...")
veh_img_list = glob.glob("./dataset/vehicles/**/*.png")
non_veh_img_list = glob.glob("./dataset/non-vehicles/**/*.png")
print("Numer of vehicle images: {}".format(len(veh_img_list)))
print("Number of non-vehicle images: {}".format(len(non_veh_img_list)))

# Extract features from list of images
print("Extracting vechicle features...")
veh_features = extract.extract_features(veh_img_list,
                                        color_space=color_space,
                                        spatial_size=spatial_size,
                                        hist_bins=hist_bins,
                                        orient=orient,
                                        pix_per_cell=pix_per_cell,
                                        cell_per_block=cell_per_block,
                                        hog_channel=hog_channel,
                                        spatial_feat=spatial_feat,
                                        hist_feat=hist_feat,
                                        hog_feat=hog_feat)
print("Extracting non-vehicle features...")
non_veh_features = extract.extract_features(non_veh_img_list,
                                            color_space=color_space,
                                            spatial_size=spatial_size,
                                            hist_bins=hist_bins,
                                            orient=orient,
                                            pix_per_cell=pix_per_cell,
                                            cell_per_block=cell_per_block,
                                            hog_channel=hog_channel,
                                            spatial_feat=spatial_feat,
 def match(self, image_path, topn=5):
     features = extract.extract_features(image_path)
     img_distances = self.cos_cdist(features)
     nearest_ids = np.argsort(img_distances).tolist()
     nearest_img_paths = self.names[nearest_ids].tolist()
     return nearest_img_paths, img_distances[nearest_ids].tolist(), nearest_ids
Exemplo n.º 9
0
def train(tfrecords, bbox_priors, logdir, cfg, pretrained_model_path=None, fine_tune=False, extract_feats=False, trainable_scopes=None, use_moving_averages=False, restore_moving_averages=False):
  """
  Args:
    tfrecords (list)
    bbox_priors (np.array)
    logdir (str)
    cfg (EasyDict)
    pretrained_model_path (str) : path to a pretrained Inception Network
  """
  tf.logging.set_verbosity(tf.logging.DEBUG)

  if extract_feats:
    cfg.DO_RANDOM_BBOX_SHIFT = 0
    cfg.DO_RANDOM_CROP = 0
    cfg.DO_RANDOM_FLIP_LEFT_RIGHT = False
    feature_cache_dir = os.path.join(logdir, "feature_cache")
    feature_cache_file = os.path.join(logdir, "feature_cache_created")
    if not os.path.exists(feature_cache_dir):
      os.makedirs(feature_cache_dir)
    if not os.path.exists(feature_cache_file):
      h,w,c = extract_features(tfrecords, pretrained_model_path, [INCEPTION_FEATURE_LAYER_NAME], cfg, feature_cache_dir)
      with open(feature_cache_file,'w') as f: f.write("%d %d %d" % (h,w,c))
    tfrecords_feat = [os.path.join(feature_cache_dir, f) for f in os.listdir(feature_cache_dir) if os.path.isfile(os.path.join(feature_cache_dir, f))]
    with open(feature_cache_file) as f: feat_shape = [int(a) for a in f.read().split(' ')]

  graph = tf.Graph()

  # Force all Variables to reside on the CPU.
  with graph.as_default():
    
    # Create a variable to count the number of train() calls. 
    global_step = slim.get_or_create_global_step()

    # Calculate the learning rate schedule.
    num_batches_per_epoch = (cfg.NUM_TRAIN_EXAMPLES /
                             cfg.BATCH_SIZE)
    decay_steps = int(num_batches_per_epoch * cfg.NUM_EPOCHS_PER_DELAY)

    # Decay the learning rate exponentially based on the number of steps.
    lr = tf.train.exponential_decay(
      learning_rate=cfg.INITIAL_LEARNING_RATE,
      global_step=global_step,
      decay_steps=decay_steps,
      decay_rate=cfg.LEARNING_RATE_DECAY_FACTOR,
      staircase=cfg.LEARNING_RATE_STAIRCASE
    )

    # Create an optimizer that performs gradient descent.
    optimizer = tf.train.RMSPropOptimizer(
      learning_rate=lr,
      decay=cfg.RMSPROP_DECAY,
      momentum=cfg.RMSPROP_MOMENTUM,
      epsilon=cfg.RMSPROP_EPSILON
    )

    input_summaries = copy.copy(tf.get_collection(tf.GraphKeys.SUMMARIES))
      
    batched_images, batched_bboxes, batched_num_bboxes, image_ids, batched_filenames = inputs.input_nodes(
        tfrecords=tfrecords,
        max_num_bboxes = cfg.MAX_NUM_BBOXES,
        num_epochs=None,
        batch_size=cfg.BATCH_SIZE,
        num_threads=cfg.NUM_INPUT_THREADS,
        capacity=cfg.QUEUE_CAPACITY,
        min_after_dequeue=cfg.QUEUE_MIN,
        add_summaries = True,
        shuffle_batch=True,
        cfg=cfg
    )
    
    
    if extract_feats:
      batched_features, batched_bboxes, batched_num_bboxes, image_ids = inputs.input_nodes_precomputed_features(
        tfrecords=tfrecords_feat,
        max_num_bboxes = cfg.MAX_NUM_BBOXES,
        num_epochs=None,
        batch_size=cfg.BATCH_SIZE,
        num_threads=cfg.NUM_INPUT_THREADS,
        capacity=cfg.QUEUE_CAPACITY,
        min_after_dequeue=cfg.QUEUE_MIN,
        shuffle_batch=True,
        cfg=cfg,
        feat_shape=feat_shape
      )
      locs, confs, inception_vars, detection_vars = build_finetunable_model(cfg, inputs=batched_images, feature_inputs=batched_features)
      all_trainable_var_names = [v.op.name for v in tf.trainable_variables()]
      trainable_vars = [v for v_name, v in detection_vars.items() if v_name in all_trainable_var_names]
    else:
      if fine_tune: 
        locs, confs, inception_vars, detection_vars = build_finetunable_model(cfg, inputs=batched_images)
        all_trainable_var_names = [v.op.name for v in tf.trainable_variables()]
        trainable_vars = [v for v_name, v in detection_vars.items() if v_name in all_trainable_var_names]
      else:
        locs, confs, inception_vars = build_fully_trainable_model(batched_images, cfg)
        trainable_vars = tf.trainable_variables()

    location_loss, confidence_loss = loss.add_loss(
      locations = locs, 
      confidences = confs, 
      batched_bboxes = batched_bboxes, 
      batched_num_bboxes = batched_num_bboxes, 
      bbox_priors = bbox_priors, 
      location_loss_alpha = cfg.LOCATION_LOSS_ALPHA
    )
    
    total_loss = slim.losses.get_total_loss()

    # Track the moving averages of all trainable variables.
    # At test time we'll restore all variables with the average value
    # Note that we maintain a "double-average" of the BatchNormalization
    # global statistics. This is more complicated then need be but we employ
    # this for backward-compatibility with our previous models.
    ema = tf.train.ExponentialMovingAverage(
      decay=cfg.MOVING_AVERAGE_DECAY,
      num_updates=global_step
    )
    variables_to_average = (slim.get_model_variables()) # Makes it easier to restore for eval and detect purposes (whether you use the fine_tune flag or not)
    maintain_averages_op = ema.apply(variables_to_average)
    tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, maintain_averages_op)

    trainable_vars = filter_trainable_variables(trainable_vars, trainable_scopes)

    train_op = slim.learning.create_train_op(total_loss, optimizer, variables_to_train=trainable_vars)

    # Summary operations
    summary_op = tf.summary.merge([
      tf.summary.scalar('total_loss', total_loss),
      tf.summary.scalar('location_loss', location_loss),
      tf.summary.scalar('confidence_loss', confidence_loss),
      tf.summary.scalar('learning_rate', lr)
    ] + input_summaries)

    sess_config = tf.ConfigProto(
      log_device_placement=False,
      #device_filters = device_filters,
      allow_soft_placement = True,
      gpu_options = tf.GPUOptions(
          per_process_gpu_memory_fraction=cfg.SESSION_CONFIG.PER_PROCESS_GPU_MEMORY_FRACTION
      )
    )

    saver = tf.train.Saver(
      # Save all variables
      max_to_keep = cfg.MAX_TO_KEEP,
      keep_checkpoint_every_n_hours = cfg.KEEP_CHECKPOINT_EVERY_N_HOURS
    )

    # Run training.
    slim.learning.train(train_op, logdir, 
      init_fn=get_init_function(logdir, pretrained_model_path, fine_tune, inception_vars, use_moving_averages, restore_moving_averages, ema),
      number_of_steps=cfg.NUM_TRAIN_ITERATIONS,
      save_summaries_secs=cfg.SAVE_SUMMARY_SECS,
      save_interval_secs=cfg.SAVE_INTERVAL_SECS,
      saver=saver,
      session_config=sess_config,
      summary_op = summary_op,
      log_every_n_steps = cfg.LOG_EVERY_N_STEPS
    )