예제 #1
0
    def write_csv_files(self):
        files = glob(os.path.join(self.image_dir, "*.jpg"))
        files.sort()
        if self.val_frac != 1.0:
            filemap, idmap, x1map, y1map, x2map, y2map = read_labels(
                self.image_dir, self.points1_file, self.points2_file, self.target_size
            )
        if self.id_label == 1:
            self.label_names = ["id"]
        else:
            self.label_names = ["x1", "y1", "x2", "y2"]

        indexes = range(len(self.label_names))
        self.label_dict = {k: v for k, v in zip(self.label_names, indexes)}

        tlines = []
        vlines = []

        np.random.shuffle(files)
        v_idx = int(self.val_frac * len(files))
        tfiles = files[v_idx:]
        vfiles = files[:v_idx]
        vfiles.sort()
        if self.id_label == 1:
            if self.val_frac == 1.0:
                vlines = [(f, 0) for f in vfiles]
            else:
                tlines = [(f, idmap[filemap[f]]) for f in tfiles]
        else:
            if self.val_frac == 1.0:
                vlines = [(f, 0, 0, 0, 0) for f in vfiles]
            else:
                tlines = [(f, x1map[f], y1map[f], x2map[f], y2map[f]) for f in tfiles]
        np.random.shuffle(tlines)

        if not os.path.exists(self.out_dir):
            os.makedirs(self.out_dir)

        for ff, ll in zip([self.train_file, self.val_file], [tlines, vlines]):
            with open(ff, "wb") as f:
                if self.id_label == 1:
                    f.write("filename,id\n")
                    for tup in ll:
                        f.write("{},{}\n".format(*tup))
                else:
                    f.write("filename,x,y\n")
                    for tup in ll:
                        f.write("{},{},{},{},{}\n".format(*tup))

        self.train_nrec = len(tlines)
        self.ntrain = -(-self.train_nrec // self.macro_size)
        self.train_start = 0

        self.val_nrec = len(vlines)
        self.nval = -(-self.val_nrec // self.macro_size)
        if self.ntrain == 0:
            self.val_start = 100
        else:
            self.val_start = 10 ** int(np.log10(self.ntrain * 10))
예제 #2
0
 def __init__(self, callback_data, model, dataset, traindir, imwidth,
              epochs, datadir, pointnum):
     super(Evaluator, self).__init__()
     self.model = model
     self.dataset = dataset
     self.callback_data = callback_data
     self.imwidth = imwidth
     self.epochs = epochs
     self.min_dist = 4 * imwidth
     traindir = os.path.join(os.path.dirname(datadir), traindir)
     _, _, x1map, y1map, x2map, y2map = read_labels(
         traindir, 'points1.json', 'points2.json', imwidth)
     xmap, ymap = (x1map, y1map) if pointnum == 1 else (x2map, y2map)
     self.xymean = np.array([np.mean(xmap.values()),
                             np.mean(ymap.values())])
예제 #3
0
 def __init__(self, callback_data, model, dataset, imwidth, epochs, datadir,
              pointnum):
     super(Evaluator, self).__init__()
     self.model = model
     self.dataset = dataset
     self.callback_data = callback_data
     self.imwidth = imwidth
     self.epochs = epochs
     self.min_dist = 4 * imwidth
     traindir = os.path.join(os.path.dirname(datadir), 'train')
     _, _, x1map, y1map, x2map, y2map = read_labels(traindir,
                                                    'points1.json',
                                                    'points2.json', imwidth)
     xmap, ymap = (x1map, y1map) if pointnum == 1 else (x2map, y2map)
     self.xymean = np.array(
         [np.mean(xmap.values()),
          np.mean(ymap.values())])
def get_features_labels(data_set, wavelet_transform=0):

    parent_dir = 'heartbeat-sounds'
    training_set = data_set + '_training'
    testing_set = data_set + '_testing'
    CSV = data_set + '.csv'

    if wavelet_transform == 1:
        print('Performing Wavelet Transformation')

    tr_sub_dirs = [training_set]
    ts_sub_dirs = [testing_set]
    new_labels, label_array = labels.read_labels(CSV)

    tr_features, tr_labels = parse_audio_files(parent_dir, tr_sub_dirs,
                                               new_labels, wavelet_transform)
    ts_features, ts_labels = parse_audio_files(parent_dir, ts_sub_dirs,
                                               new_labels, wavelet_transform)

    tr_labels = one_hot_encode(tr_labels)
    ts_labels = one_hot_encode(ts_labels)

    return tr_features, tr_labels, ts_features, ts_labels
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping
from keras.utils import np_utils
from keras.regularizers import l2
import labels
import wavelet_transformation

directory = 'set_'+sys.argv[1]
wavelet_transform = int(sys.argv[2])

INPUT_LIB = 'heartbeat-sounds/'
SAMPLE_RATE = 4000
CSV = directory+'.csv'
DATASET = directory+'/'

new_labels,label_array = labels.read_labels(CSV)

def clean_filename(fname, string):
    file_name = fname.split('/')[1]
    if file_name[:2] == '__':
        file_name = string + file_name
    return file_name

def load_wav_file(name, path, wavelet_transform=1):
    _, audio_data = wavfile.read(path + name)
    #print(_)
    #assert _ == SAMPLE_RATE
    if wavelet_transform == 1:
        audio_data = wavelet_transformation.wavelet_transformation(audio_data)
    return audio_data
예제 #6
0
    import audios
    import coders
    import labels
    import videos
    import youtube

    if args.loop or args.play:
        coders.write_video_batch()
        sys.exit()

    with multiprocessing.Manager() as manager:
        labels.labels = manager.list()
        videos.videos = manager.dict()
        random.seed(int.from_bytes(os.getrandom(4), 'big'))
        if not args.labels_reinit:
            labels.read_labels()
        else:
            if args.input_labels:
                labels.read_labels(args.input_labels)
            else:
                if args.labels_from_input:
                    assert args.input
                    created_labels = videos.labels_from_video(args.input)
                    labels.update_labels(created_labels)
            labels.write_labels()
        if args.audios:
            args.audios[:] = audios.read_audios()
            if not labels.labels:
                if args.labels_public:
                    created_labels = youtube.obtain_labels(args.audios[0])
                if not args.labels_public or not created_labels: