def _precomputeProbabilities(self):
        print("Pre-Computing likelihoods...")
        result = []
        progressCounter = 0

        # Unique pairs of Word/Class
        uniquePairs = self.trainData.loc[:, ['Class','Word']].drop_duplicates()

        for index, row in uniquePairs.iterrows():
            Class = row['Class']
            Word = row['Word']

            df = self.trainData[
                (self.trainData['Class'] == Class) &
                (self.trainData['Word'] == Word)]
            count = df['Count'].sum()

            df = self.trainData[self.trainData['Class'] == Class]
            total = df['Count'].sum()
            likelihood = (float(count) + 1)/(float(total) + self.sizeOfVocab)

            result.append({
                'Class':Class,
                'Word':Word,
                'P': math.log(likelihood)
            })

            # Print the progress
            progressCounter += 1
            util.printProgress(progressCounter, len(uniquePairs))

        return pandas.DataFrame(result)
def train(model_name, gpu_id):
    params = param.get_general_params()

    network_dir = params['model_save_dir'] + '/' + model_name

    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    train_feed = data_generation.create_feed(params, params['data_dir'],
                                             'train')

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    vgg_model = truncated_vgg.vgg_norm()
    networks.make_trainable(vgg_model, False)
    response_weights = sio.loadmat(
        'posewarp-cvpr2018/data/vgg_activation_distribution_train.mat')
    model = networks.network_posewarp(params)
    model.compile(optimizer=Adam(lr=1e-4),
                  loss=[networks.vgg_loss(vgg_model, response_weights, 12)])
    model.load_weights(network_dir + '/' + 'weights_gan_model.h5')
    model.summary()
    n_iters = params['n_training_iter']
    for step in range(0, n_iters):
        x, y = next(train_feed)
        train_loss = model.train_on_batch(x, y)
        util.printProgress('Iteration: ', step, ' Train_loss: ', train_loss)

        if step > 0 and step % params['model_save_interval'] == 0:
            model.save_weights(
                (network_dir + '/' + 'weights_model_gan_improved' + '.h5'))
Exemple #3
0
def trafficAssignment(net,demands,args):
    print('Conducting Traffic Assignment...')
    time_start = time.time()
    netgraph = nx.DiGraph()
    total_assignment_iteration = args['assignment_iters']

    for mesolink in net.meso_link_list:
        mesolink.flow_volume = 0
        mesolink.CalculateBPRFunctionAndCost()
        netgraph.add_edge(mesolink.from_node_id, mesolink.to_node_id, weight=mesolink.cost)

    printProgress(1, total_assignment_iteration, width=30)
    _findPathForAgents(demands, net, netgraph, 0)

    for i in range(1, total_assignment_iteration):
        for mesolink in net.meso_link_list:
            mesolink.CalculateBPRFunctionAndCost()
            netgraph[mesolink.from_node_id][mesolink.to_node_id]['weight'] = mesolink.cost
            mesolink.flow_volume = 0

        printProgress(i+1, total_assignment_iteration, width=30)
        _findPathForAgents(demands, net, netgraph, i)
    print()
    time_end = time.time()
    print(f'time used for traffic assignment: {round(time_end-time_start,3)} seconds')
Exemple #4
0
def train(model_name, gpu_id):

    params = param.getGeneralParams()

    gpu = '/gpu:' + str(gpu_id)

    network_dir = params['project_dir'] + '/results/networks/' + model_name

    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    train_feed = datageneration.createFeed(params, "train_vids.txt")
    #test_feed=datageneration.createFeed(params,"test_vids.txt")

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True
    set_session(tf.Session(config=config))

    with tf.device(gpu):
        vgg_model = myVGG.vgg_norm()
        networks.make_trainable(vgg_model, False)
        response_weights = sio.loadmat('mean_response.mat')
        model = networks.network_fgbg(params)
        #model.load_weights('../results/networks/fgbg_vgg/60000.h5')
        model.compile(optimizer=Adam(lr=1e-4),
                      loss=[networks.vggLoss(vgg_model, response_weights)])

    #model.summary()

    for step in xrange(0, 250000):
        start = time.time()

        #X,Y = next(test_feed)
        #sio.savemat('data/data' + str(step) + '.mat',{'X':X[0],'Y':Y, 'ps': X[1], 'pt': X[2], 'mask': X[3]})
        #return

        X, Y = next(train_feed)

        with tf.device(gpu):
            train_loss = model.train_on_batch(X, Y)

        end = time.time()

        util.printProgress(step, 0, train_loss, end - start)
        '''
		if(step % params['test_interval'] == 0):
			n_batches = 8
			test_loss = 0
			for j in xrange(n_batches):	
				X,Y = next(test_feed)			
				test_loss += np.array(model.test_on_batch(X,Y))
			
			test_loss /= (n_batches)
			util.printProgress(step,1,test_loss,0)
		'''

        if (step > 0 and step % params['model_save_interval'] == 0):
            model.save(network_dir + '/' + str(step) + '.h5')
Exemple #5
0
    def parse(self):
        data = {}
        self.search(self.currentDir)
        self.genImgDirDict(self.currentDir)
        self.length = len(self.xmlFiles)
        printProgress(self.progressCnt, self.length, ' AIHub Parsing Progress: ', 'Complete')

        for xml in self.xmlFiles:
            doc = ET.parse(xml)
            root = doc.getroot()

            for image in root.findall('image'):
                #ran = random.randrange(0,3)
                #if ran != 1:
                #    continue
                filename = image.attrib['name']
                type = 'valid' if random.randrange(0, 10) == 0 else 'train'
                size = {
                    "width": float(image.attrib['width']),
                    "height": float(image.attrib['height'])
                }
                info = {
                    "path": self.imgDir[filename],
                    "type": type,
                    "ignore": None
                }
                for box in image.iter('box'):
                    bbox = {
                        "xmin": float(box.attrib['xtl']),
                        "ymin": float(box.attrib['ytl']),
                        "xmax": float(box.attrib['xbr']),
                        "ymax": float(box.attrib['ybr'])
                    }

                    bboxInfo = {
                        "name": box.attrib['label'],
                        "bbox": bbox
                    }

                    if filename in data:
                        obj_idx = data[filename]["objects"]["num_obj"] + 1
                        data[filename]["objects"][str(obj_idx)] = bboxInfo
                        data[filename]["objects"]["num_obj"] += 1
                    else:
                        obj = {
                            "num_obj": 1,
                            "1": bboxInfo
                        }
                        data[filename] = {
                            "info": info,
                            "size": size,
                            "objects": obj
                        }
                    bbox = {}
                    bboxInfo = {}

            self.progressCnt += 1
            printProgress(self.progressCnt, self.length, ' AIHub Parsing Progress: ', 'Complete')
        return data
Exemple #6
0
    def parse(self):
        data = {}
        type = 'valid' if random.randrange(0, 10) == 0 else 'train'
        self.progressCnt = 0
        labels = os.listdir(self.labelDir)
        length = len(labels)
        printProgress(self.progressCnt, length, ' Kitti Parsing Progress {}: '.format(type), 'Complete')

        for label in labels:
            fName, fextension = os.path.splitext(label)
            filename = fName + ".png"
            img = cv2.imread(os.path.join(self.imgDir, fName + ".png"))
            imgHeight, imgWidth, _ = img.shape
            size = {
                "width": imgWidth,
                "height": imgHeight
            }

            with open(os.path.join(self.labelDir, label), 'r') as f:
                lines = f.readlines()
                for line in lines:
                    line = line.split(' ')
                    info = {
                        "path": self.imgDir,
                        "type": type,
                        "ignore": None
                    }

                    bbox = {
                        "xmin": int(float(line[4])),
                        "ymin": int(float(line[5])),
                        "xmax": int(float(line[6])),
                        "ymax": int(float(line[7]))
                    }

                    bboxInfo = {
                        "name": line[0],
                        "bbox": bbox
                    }

                    if filename in data:
                        obj_idx = data[filename]["objects"]["num_obj"] + 1
                        data[filename]["objects"][str(obj_idx)] = bboxInfo
                        data[filename]["objects"]["num_obj"] += 1
                    else:
                        obj = {
                            "num_obj": 1,
                            "1": bboxInfo
                        }

                        data[filename] = {
                            "info": info,
                            "size": size,
                            "objects": obj
                        }
            self.progressCnt += 1
            printProgress(self.progressCnt, length, ' Kitti Parsing Progress: {}'.format(type), 'Complete')
        return data
Exemple #7
0
    def fetchCardPrice(self, card, page=0, cheapestPrice=None):
        pageSize = 30

        # URL will not accept encoded ', it needs non-UTF8 ´
        name = card.name.replace('\'', '´').replace('\"', '„').encode(
            "Windows-1252", "ignore")

        response = requests.post(self.baseUrl,
                                 data={
                                     'searchname': name,
                                     'searchtype': 'card',
                                     'akce': '3',
                                     'limit': page * pageSize
                                 },
                                 params={
                                     'searchname': name,
                                     'searchtype': 'card',
                                     'akce': '3',
                                     'limit': page * pageSize
                                 })

        regex = '<font style="font-weight : bolder;">(.*?)</font>'

        items = re.findall(regex, response.text)

        names = items[0::3]
        prices = items[2::3]

        for name, price in zip(names, prices):

            ends = [
                ' - foil', ' - lightly played', ' / lightly played',
                ' - played', ' / played', ' / non-english',
                ' - played / japanese', ' / japanese', ' - japanese',
                ' - non-english', ' / chinese', ' / russian', ' - russian'
            ]

            for end in ends:
                name = name[::-1].replace(end[::-1], '', 1)[::-1]

            name = name.split(' (')[0]

            name = name.replace('´', '\'').replace('\x84', '\"')

            price = int(price.split('&')[0])

            if ((cheapestPrice == None or cheapestPrice > price)
                    and name.lower() == card.name.lower()):
                cheapestPrice = price

        if (len(names) == pageSize and 'Nalezeno' in response.text):
            util.printProgress(page)
            return self.fetchCardPrice(card,
                                       page=page + 1,
                                       cheapestPrice=cheapestPrice)
        else:

            return cheapestPrice
Exemple #8
0
def test(model_name, gpu_id):

    with tf.Session() as sess:

        params = param.get_general_params()

        network_dir = params['model_save_dir'] + '/' + model_name

        # Creates models directory if not exist.
        if not os.path.isdir(network_dir):
            print(network_dir)
            os.mkdir(network_dir)

        test_feed = data_generation.create_feed(params, params['data_dir'], 'test')

        os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        set_session(tf.Session(config=config))

        vgg_model = truncated_vgg.vgg_norm()
        networks.make_trainable(vgg_model, False)
        response_weights = sio.loadmat('../data/vgg_activation_distribution_train.mat')

        ckp_name = [f for f in listdir(network_dir) if isfile(join(network_dir, f))][-1]

        model = networks.network_posewarp(params)
        model.compile(optimizer=Adam(lr=1e-4), loss=[networks.vgg_loss(vgg_model, response_weights, 12)])

        model.load_weights(network_dir + "/"  + ckp_name)

        n_iters = 100

        summary_writer = tf.summary.FileWriter("D:\Proyectos\JEJU2018\Code\posewarp-cvpr2018\code\logs", graph=sess.graph)

        for step in range(0, n_iters):

            x, y = next(test_feed)

            test_loss = model.test_on_batch(x, y)
            util.printProgress(step, 0, test_loss)

            # out = sess.run(conv, feed_dict={"input_1:0" : x[0]})
            # plt.matshow(out[0, :, :, 0])
            # plt.show()

            gen = tf.get_default_graph().get_tensor_by_name("loss/add_2_loss/lambda_5/add:0")
            inp = tf.get_default_graph().get_tensor_by_name("in_img0:0")
            out = tf.get_default_graph().get_tensor_by_name("in_img1:0")
            image_summary_op = tf.summary.image('images', [inp[0, :, :, :], out[0, :, :, :], gen[0, :, :, :]], max_outputs=100)
            image_summary = sess.run(image_summary_op, feed_dict={"in_img0:0" : x[0], "in_pose0:0" : x[1], "in_pose1:0" : x[2], "mask_prior:0" : x[3], "trans_in:0" : x[4], "in_img1:0" : y})
            summary_writer.add_summary(image_summary)
    def factorize(self):
        result = list()
        for i in range(2, self.factor):
            if self.factor % i == 0:
                result.append(i)
                secondResult = int(self.factor / i)
                result.append(secondResult)
                break

            # Visualisierung der noch zu testenden Faktoren
            if i % 1000 == 2:
                printProgress(i, self.factor)
        print("\n")
        return result[0], result[1]
def train(model_name, gpu_id):
    params = param.get_general_params()

    network_dir = params['model_save_dir'] + '/' + model_name
    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    train_feed = data_generation.create_feed(params, params['data_dir'],
                                             'train')

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    vgg_model = truncated_vgg.vgg_norm()
    networks.make_trainable(vgg_model, False)
    response_weights = sio.loadmat(params['data_dir'] +
                                   '/vgg_activation_distribution_train.mat')
    model = networks.network_posewarp(params)
    if not params['load_weights'] == None:
        model.load_weights(params['load_weights'])
    alpha = 0.4
    #model.compile(optimizer=Adam(lr=1e-4), loss=[networks.vgg_loss(vgg_model, response_weights, 12)])
    model.compile(
        optimizer=Adam(lr=1e-4),
        loss=lambda y_true, y_pred:
        (1 - alpha) * networks.vgg_loss(vgg_model, response_weights, 12)
        (y_true, y_pred) + alpha * tf.reduce_mean(tf.square(y_pred - y_true)))

    #model.summary()
    n_iters = params['n_training_iter']
    loss_note = []
    if params['load_weights'] == None:
        start = 0
    else:
        start = int(params['load_weights'].split("/")[-1][:-3])

    for step in range(start, n_iters):
        x, y = next(train_feed)

        train_loss = model.train_on_batch(x, y)
        loss_note.append([str(step), str(train_loss)])
        util.printProgress(step, 0, train_loss)
        if step > 0 and step % params['model_save_interval'] == 0:
            model.save(network_dir + '/' + str(step) + '.h5')
            pd.DataFrame(loss_note).to_csv(network_dir + f"/{step}.csv",
                                           header=None,
                                           index=None)
            loss_note = []
Exemple #11
0
 def analysis(self, data):
     self.length = len(data)
     printProgress(self.progressCnt, self.length, ' Analysis Progress: ',
                   'Complete')
     for key in data:
         temp = [False for i in range(5)]
         for idx in range(1, data[key]["objects"]["num_obj"] + 1):
             cls = data[key]["objects"][str(idx)]["name"]
             if cls in self.labelDict:
                 self.config.classNum[self.labelDict[cls]] += 1
                 temp[self.labelDict[cls]] = True
         for i, t in enumerate(temp):
             if t:
                 self.config.imgNum[i] += 1
         self.progressCnt += 1
         printProgress(self.progressCnt, self.length,
                       ' Analysis Progress: ', 'Complete')
Exemple #12
0
def train(model_name, gpu_id, start_iter=0):
    params = param.get_general_params()

    network_dir = params['model_save_dir'] + '/' + model_name

    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    tf_writer = tf.summary.FileWriter(network_dir + "/log/")

    train_feed = data_generation.create_feed(params, None, ModelMode.train)

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    vgg_model = truncated_vgg.vgg_norm()
    networks.make_trainable(vgg_model, False)
    response_weights = sio.loadmat(
        '../data/vgg_activation_distribution_train.mat')
    model = networks.network_posewarp(params)
    model.load_weights('../models/vgg_100000.h5')
    model.compile(optimizer=Adam(lr=1e-4),
                  loss=[networks.vgg_loss(vgg_model, response_weights, 12)])

    #model.summary()
    n_iters = params['n_training_iter']

    for step in range(start_iter, n_iters + 1):
        x, y = next(train_feed)

        train_loss = model.train_on_batch(x, y)

        util.printProgress(step, 0, train_loss)
        summary = tf.Summary(value=[
            tf.Summary.Value(tag="train_loss", simple_value=train_loss)
        ])
        tf_writer.add_summary(summary, step)

        if step > 0 and step % 100 == 0:
            tf_writer.flush()

        if step > 0 and step % params['model_save_interval'] == 0:
            model.save(network_dir + '/' + str(step) + '.h5')
    model.save(network_dir + '/' + str(step) + '.h5')
Exemple #13
0
def train(model_name, gpu_id):
    params = param.get_general_params()

    network_dir = params['model_save_dir'] + '/' + model_name

    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    train_feed = data_generation.create_feed(params, params['data_dir'],
                                             'train')

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    vgg_model = truncated_vgg.vgg_norm()
    networks.make_trainable(vgg_model, False)
    response_weights = sio.loadmat(
        '../data/vgg_activation_distribution_train.mat')
    model = networks.network_posewarp(params)
    model.compile(optimizer=Adam(lr=1e-4),
                  loss=[networks.vgg_loss(vgg_model, response_weights, 12)])

    #model.summary()
    n_iters = params['n_training_iter']

    log_dir = '../log/{:s}'.format(model_name)
    callback = TensorBoard(log_dir, write_graph=True)
    callback.set_model(model)
    train_names = ['train_loss']

    for step in range(0, n_iters):
        x, y = next(train_feed)

        train_loss = model.train_on_batch(x, y)

        util.printProgress(step, 0, train_loss)
        write_log(callback, train_names, [train_loss], step)

        if step > 0 and step % params['model_save_interval'] == 0:
            model.save(network_dir + '/' + str(step) + '.h5')
    def execute(self, testData):
        self.testData = testData
        result = []
        Ids = testData['Id'].unique()
        progressCounter = 0

        print("Predicting...")
        # Make a prediction for each emailId in the test set
        for emailId in Ids:
            example = testData[testData['Id'] == emailId]
            actualClass = example['Class'].values[0]
            predicted = self.predict(example)

            if predicted['pHam'] > predicted['pSpam']:
                predictedClass = "ham"
            else:
                predictedClass = "spam"

            result.append({
                'ActualClass':actualClass,
                'PredictedClass':predictedClass,
                'pHam':predicted['pHam'],
                'pSpam':predicted['pSpam']
            })

            # Print the progress
            progressCounter += 1
            util.printProgress(progressCounter, len(Ids))

        resultDF = pandas.DataFrame(result)

        # Calculate accuracy
        countCorrect = len(resultDF[
            resultDF['ActualClass'] == resultDF['PredictedClass']])
        totalRows = len(resultDF)
        print("Accuracy: {0}".format(countCorrect/totalRows))
Exemple #15
0
    def yolo(self, data, save, copy):
        self.length = len(data)
        if copy:
            copyDir = os.path.join(self.config.copyDir, self.config.datasets)
            if not os.path.exists(copyDir):
                os.makedirs(copyDir)

        printProgress(self.progressCnt, self.length,
                      ' YOLO Parsing Progress: ', 'Complete')
        fname = None
        for key in data:
            bExcept = False
            outputStr = ''
            fileName, fextension = os.path.splitext(key)
            fname = fileName.split('-')[0]
            path = data[key]["info"]["path"]
            type = data[key]["info"]["type"]
            ignore = None
            # ignore = data[key]["info"]["ignore"]
            imgWidth = data[key]["size"]["width"]
            imgHeight = data[key]["size"]["height"]

            if not copy:
                copyDir = path
            for idx in range(1, data[key]["objects"]["num_obj"] + 1):
                cls = data[key]["objects"][str(idx)]["name"]
                xmin = data[key]["objects"][str(idx)]["bbox"]["xmin"]
                ymin = data[key]["objects"][str(idx)]["bbox"]["ymin"]
                xmax = data[key]["objects"][str(idx)]["bbox"]["xmax"]
                ymax = data[key]["objects"][str(idx)]["bbox"]["ymax"]

                bboxCoordinate = self.coordinateConvert(
                    "xyMinMax", [imgWidth, imgHeight, xmin, ymin, xmax, ymax])
                if cls in self.labelDict:
                    outputStr += "{} {} {} {} {}\n".format(
                        self.labelDict[cls], bboxCoordinate[0],
                        bboxCoordinate[1], bboxCoordinate[2],
                        bboxCoordinate[3])  # using join
                """    if (self.isPerson(cls) == True and (bboxCoordinate[2] >= 0.3 or bboxCoordinate[3] >= 0.5 or bboxCoordinate[2] > bboxCoordinate[3] * 1.5)):
                        bExcept = True
                        print("[Info]  File {} is excepted.".format(fileName))
                """

            imgFile = os.path.join(
                path,
                key)  #imgFile = ''.join(path.split('.')[:-1]) + fextension
            # except
            if bExcept == True: continue
            if outputStr != '' or self.config.negative:
                if save:
                    copyDir2 = os.path.join(
                        self.config.copyDir, 'label',
                        self.config.datasets + '-label_txt')
                    if not os.path.exists(copyDir2):
                        os.makedirs(copyDir2)
                    textFile = os.path.join(
                        copyDir2, fileName + '.txt'
                    )  #textFile = ''.join(path.split('.')[:-1]) + '.txt'
                    # textFile = os.path.join(copyDir, fileName + '.txt') #textFile = ''.join(path.split('.')[:-1]) + '.txt'
                    with open(textFile, 'w+') as labelFile:
                        labelFile.write(outputStr)
                    if copy:
                        # Generative masked images. (detarc, Visdron)
                        if ignore is not None:
                            img = cv2.imread(imgFile)
                            for box in ignore:
                                cv2.rectangle(
                                    img, (int(box["xmin"]), int(box["ymin"])),
                                    (int(box["xmax"]), int(box["ymax"])),
                                    (102, 115, 123), -1)
                            cv2.imwrite(os.path.join(copyDir, key), img)
                        else:
                            shutil.copy(imgFile, os.path.join(copyDir, key))
                else:
                    textFile = os.path.join(path, fileName + '.txt')
                    print("[Info]  textFile {} \noutputStr {}".format(
                        textFile, outputStr))

                if type == 'train':
                    self.trainList += "{}\n".format(os.path.join(copyDir, key))
                else:
                    self.validList += "{}\n".format(os.path.join(copyDir, key))
            self.progressCnt += 1
            printProgress(self.progressCnt, self.length,
                          ' YOLO Parsing Progress: ', 'Complete')

        if save:
            with open(
                    os.path.join(
                        os.getcwd(),
                        self.config.datasets + "-train-" + self.manipastFile),
                    "w+") as manipastFile:
                # with open(os.path.join(copyDir, "train-" + self.manipastFile), "a+") as manipastFile:
                manipastFile.write(self.trainList)
            with open(
                    os.path.join(
                        os.getcwd(),
                        self.config.datasets + "-valid-" + self.manipastFile),
                    "w+") as manipastFile:
                # with open(os.path.join(copyDir, "valid-" + self.manipastFile), "a+") as manipastFile:
                manipastFile.write(self.validList)
            print("[Info] Save the manipastFile. ({})".format(
                os.path.join(os.getcwd(), self.config.datasets + ' - ' +
                             self.manipastFile)))
def train(model_name, gpu_id):

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    with tf.Session() as sess:

        params = param.get_general_params()

        network_dir = params['model_save_dir'] + '/' + model_name

        # Creates models directory if not exist.
        if not os.path.isdir(network_dir):
            os.mkdir(network_dir)

        train_feed = data_generation.create_feed(params, params['data_dir'],
                                                 'train')
        # test_feed  = data_generation.create_feed(params, params['data_dir'], 'test')

        tf_config = tf.ConfigProto()
        tf_config.gpu_options.allow_growth = True
        tf_config.allow_soft_placement = True

        with tf.Session(config=tf_config) as sess:

            # Load VGG truncated model.
            vgg_model = truncated_vgg.vgg_norm()
            networks.make_trainable(vgg_model, False)

            # Load VGG pretrained weights.
            response_weights = sio.loadmat(
                '../Models/vgg_activation_distribution_train.mat')

            # Create graph and compile keras model.
            model = networks.network_posewarp(params)
            tloss = networks.vgg_loss(vgg_model, response_weights, 12)
            model.compile(optimizer=Adam(lr=1e-4), loss=[tloss])

            # Get number of trainig steps.
            n_iters = params['n_training_iter']

            # Create a tensorboard writer.
            summary_writer = tf.summary.FileWriter("./logs/run2/",
                                                   graph=sess.graph)

            tr_x, tr_y = next(train_feed)
            # te_x, te_y = next(test_feed)

            # Prepare output directories if they don't exist.
            output_dir = '../Output/' + model_name + '/'

            if not os.path.isdir(output_dir):
                os.mkdir(output_dir)

            scipy.misc.imsave('../Output/' + model_name + '/tr_orig_image.png',
                              tr_x[0][0, :, :, :])
            scipy.misc.imsave('../Output/' + model_name + '/tr_targ_image.png',
                              tr_y[0, :, :, :])

            # scipy.misc.imsave('../Output/' + model_name + '/te_orig_image.png', te_x[0][0, :, :, :])
            # scipy.misc.imsave('../Output/' + model_name + '/te_targ_image.png', te_y[0, :, :, :])

            # Tensorboard logged tensors.
            gen = tf.get_default_graph().get_tensor_by_name(
                "loss/add_2_loss/lambda_5/add:0")[0, :, :, :]
            inp = tf.get_default_graph().get_tensor_by_name("in_img0:0")[
                0, :, :, :]
            msk = tf.get_default_graph().get_tensor_by_name("mask_prior:0")[
                0, :, :, 0:1]
            msk = tf.tile(msk, [1, 1, 3])
            out = tf.get_default_graph().get_tensor_by_name("in_img1:0")[
                0, :, :, :]

            for step in range(0, n_iters):

                # Train with next batch.
                x, y = next(train_feed)

                # plt.imshow(x[0][0, :, :, 0] * 3 - cv2.resize(x[3][0, :, :, 0], (256, 256)))
                # plt.show()
                # plt.imshow(x[0][1, :, :, 0] * 3 - cv2.resize(x[3][1, :, :, 0], (256, 256)))
                # plt.show()
                train_loss = model.train_on_batch(x, y)

                # Print training loss progress.
                util.printProgress(step, 0, train_loss)

                # Add training loss to tensorboard.
                summary = tf.Summary()
                summary.value.add(tag='loss', simple_value=train_loss)
                summary_writer.add_summary(summary, step)

                # plt.imshow(np.round(((((x[3][1]) / 2.0) + 0.5) * 255.0)).astype(np.uint8))
                # plt.show()
                #
                # plt.imshow(x[3][1, :, :, 0])
                # plt.show()

                if step % params['test_interval'] == 0:

                    # Set up tensorboard image summary.
                    image_summary_1 = tf.summary.image('images',
                                                       [inp, msk, out, gen],
                                                       max_outputs=100)

                    # Compute summary.
                    image_summary_1_run = sess.run(image_summary_1,
                                                   feed_dict={
                                                       "in_img0:0": x[0],
                                                       "in_pose0:0": x[1],
                                                       "in_pose1:0": x[2],
                                                       "mask_prior:0": x[3],
                                                       "trans_in:0": x[4],
                                                       "in_img1:0": y
                                                   })
                    # Register summary in tensorboard.
                    summary_writer.add_summary(image_summary_1_run)

                    # Compute training sample images.
                    train_image = sess.run(gen,
                                           feed_dict={
                                               "in_img0:0": tr_x[0],
                                               "in_pose0:0": tr_x[1],
                                               "in_pose1:0": tr_x[2],
                                               "mask_prior:0": tr_x[3],
                                               "trans_in:0": tr_x[4],
                                               "in_img1:0": tr_y
                                           })

                    # Save in disk computed sample images.
                    scipy.misc.imsave(output_dir + 'tr' + str(step) + ".png",
                                      train_image)

                # Save model checkpoints.
                if step > 0 and step % params['model_save_interval'] == 0:
                    model.save_weights(network_dir + '/' + str(step) + '.h5')
Exemple #17
0
    def startSim(self):
        net = self.network
        agent_list = self.demands.agent_list
        number_of_agents = self.demands.number_of_agents
        signal_manager = self.signal_manager

        number_of_simu_intervals = int(
            np.ceil(
                (self.Simulation_EndTimeInMin - self.Simulation_StartTimeInMin)
                * 60 / self.number_of_seconds_per_interval))
        active_agent_list = []
        number_of_simu_interval_per_min = 60 / self.number_of_seconds_per_interval
        current_active_agent_index = 0
        safe_headway_in_simu_interval = np.round(
            self.time_headway / self.number_of_seconds_per_interval)

        departure_time_list = [
            agent.departure_time_in_simu_interval for agent in agent_list
        ]
        agent_index = np.argsort(departure_time_list)

        print('Doing Simulation...')
        time_start = time.time()

        for t in range(number_of_simu_intervals):

            if t % number_of_simu_interval_per_min == 0:

                printProgress(t * self.number_of_seconds_per_interval / 60 +
                              self.Simulation_StartTimeInMin,
                              self.Simulation_EndTimeInMin,
                              width=30)

                while current_active_agent_index < number_of_agents:
                    if agent_list[agent_index[
                            current_active_agent_index]].departure_time_in_simu_interval < t:
                        current_active_agent_index += 1
                        continue
                    if agent_list[agent_index[
                            current_active_agent_index]].departure_time_in_simu_interval >= t + number_of_simu_interval_per_min:
                        break

                    agent = agent_list[agent_index[current_active_agent_index]]
                    if agent.m_no_physical_path_flag:
                        current_active_agent_index += 1
                        continue

                    agent.m_bGenereated = True
                    agent.current_meso_path_seq_no = 0
                    agent.micro_path_node_seq_no_list.append(
                        agent.micro_origin_node_seq_no)
                    agent.micro_path_node_id_list.append(
                        agent.micro_origin_node_id)
                    agent.m_Veh_LinkArrivalTime_in_simu_interval.append(t)
                    agent.m_Veh_LinkDepartureTime_in_simu_interval.append(
                        agent.departure_time_in_simu_interval)

                    agent.current_meso_link_seq_no = agent.meso_path_link_seq_no_list[
                        agent.current_meso_path_seq_no]
                    if agent.current_meso_path_seq_no < len(
                            agent.meso_path_link_seq_no_list) - 1:
                        agent.next_meso_link_seq_no = agent.meso_path_link_seq_no_list[
                            agent.current_meso_path_seq_no + 1]
                    else:
                        agent.next_meso_link_seq_no = -1

                    active_agent_list.append(agent.agent_seq_no)
                    current_active_agent_index += 1

            agent_remove_list = []
            inactive_microlinks = signal_manager.getStatus()

            for agent_no in active_agent_list:
                agent = agent_list[agent_no]
                if agent.m_Veh_LinkDepartureTime_in_simu_interval[-1] == t:
                    current_node = net.micro_node_list[
                        agent.micro_path_node_seq_no_list[-1]]
                    if current_node.node_id in net.meso_link_list[
                            agent.
                            current_meso_link_seq_no].micro_outgoing_node_id_list:
                        if agent.current_meso_path_seq_no == len(
                                agent.meso_path_link_seq_no_list) - 1:
                            agent_remove_list.append(agent.agent_id)
                            agent.arrival_time_in_simu_interval = t
                            agent.arrival_time_in_min = t * self.number_of_seconds_per_interval / 60 + self.Simulation_StartTimeInMin
                            agent.micro_destination_node_id = current_node.node_id
                            agent.m_bCompleteTrip = True
                            agent.travel_time_in_min = agent.arrival_time_in_min - agent.departure_time_in_min
                            current_node.available_sim_interval = t
                            continue
                        else:
                            agent.current_meso_path_seq_no += 1
                            agent.current_meso_link_seq_no = agent.meso_path_link_seq_no_list[
                                agent.current_meso_path_seq_no]
                            if agent.current_meso_path_seq_no < len(
                                    agent.meso_path_link_seq_no_list) - 1:
                                agent.next_meso_link_seq_no = agent.meso_path_link_seq_no_list[
                                    agent.current_meso_path_seq_no + 1]
                            else:
                                agent.next_meso_link_seq_no = -1

                    outgoing_link_keeping = None
                    to_node_keeping = None
                    cost_keeping = 0.0

                    outgoing_link_changing_list = []
                    to_node_changing_list = []
                    cost_changing_list = []

                    for outgoing_link_id in current_node.m_outgoing_link_list:
                        outgoing_link = net.micro_link_list[
                            net.micro_link_id_to_seq_no_dict[outgoing_link_id]]
                        if net.meso_link_id_to_seq_no_dict[
                                outgoing_link.
                                meso_link_id] != agent.current_meso_link_seq_no:
                            continue
                        if outgoing_link in inactive_microlinks:
                            continue

                        if outgoing_link.cell_type == 1:
                            outgoing_link_keeping = outgoing_link
                            to_node_keeping = net.micro_node_list[
                                net.micro_node_id_to_seq_no_dict[
                                    outgoing_link.to_node_id]]

                            tree_cost = net.meso_link_list[
                                agent.
                                current_meso_link_seq_no].estimated_cost_tree_for_each_movement[
                                    agent.next_meso_link_seq_no][
                                        to_node_keeping.node_seq_no]
                            if tree_cost == _MAX_COST_TREE:
                                cost_keeping = _MAX_COST_TREE
                            elif to_node_keeping.available_sim_interval > t + outgoing_link.free_flow_travel_time_in_simu_interval:
                                cost_keeping = _MAX_TIME_INTERVAL
                            else:
                                cost_keeping = outgoing_link.free_flow_travel_time_in_simu_interval + outgoing_link.additional_cost + tree_cost

                        else:
                            outgoing_link_changing_list.append(outgoing_link)
                            to_node_changing = net.micro_node_list[
                                net.micro_node_id_to_seq_no_dict[
                                    outgoing_link.to_node_id]]

                            tree_cost = net.meso_link_list[
                                agent.
                                current_meso_link_seq_no].estimated_cost_tree_for_each_movement[
                                    agent.next_meso_link_seq_no][
                                        to_node_changing.node_seq_no]
                            if tree_cost == _MAX_COST_TREE:
                                cost_changing = _MAX_COST_TREE
                            elif to_node_changing.available_sim_interval > t + outgoing_link.free_flow_travel_time_in_simu_interval:
                                cost_changing = _MAX_TIME_INTERVAL
                            else:
                                cost_changing = outgoing_link.free_flow_travel_time_in_simu_interval + outgoing_link.additional_cost + tree_cost

                            to_node_changing_list.append(to_node_changing)
                            cost_changing_list.append(cost_changing)

                    if to_node_keeping is None:
                        agent.m_Veh_LinkDepartureTime_in_simu_interval[-1] += 1
                        continue

                    if len(cost_changing_list) == 0:

                        if cost_keeping == _MAX_COST_TREE:
                            agent_remove_list.append(agent.agent_id)
                            agent.m_bCompleteTrip = False
                            current_node.available_sim_interval = t

                        elif cost_keeping < _MAX_TIME_INTERVAL:
                            to_node_keeping.available_sim_interval = _MAX_TIME_INTERVAL
                            current_node.available_sim_interval = t + safe_headway_in_simu_interval
                            agent.micro_path_node_seq_no_list.append(
                                to_node_keeping.node_seq_no)
                            agent.micro_path_link_seq_no_list.append(
                                outgoing_link_keeping.link_seq_no)
                            agent.micro_path_node_id_list.append(
                                to_node_keeping.node_id)
                            agent.micro_path_link_id_list.append(
                                outgoing_link_keeping.link_id)
                            agent.m_Veh_LinkArrivalTime_in_simu_interval.append(
                                t)
                            agent.m_Veh_LinkDepartureTime_in_simu_interval.append(
                                t + outgoing_link_keeping.
                                free_flow_travel_time_in_simu_interval)
                        else:
                            agent.m_Veh_LinkDepartureTime_in_simu_interval[
                                -1] += 1
                            continue

                    else:
                        cost_changing = min(cost_changing_list)
                        smallest_position = cost_changing_list.index(
                            cost_changing)
                        to_node_changing = to_node_changing_list[
                            smallest_position]
                        outgoing_link_changing = outgoing_link_changing_list[
                            smallest_position]

                        if (cost_keeping == _MAX_COST_TREE) & (
                                cost_changing == _MAX_COST_TREE):
                            agent_remove_list.append(agent.agent_id)
                            agent.m_bCompleteTrip = False
                            current_node.available_sim_interval = t
                            continue

                        if (cost_keeping < _MAX_TIME_INTERVAL) & (
                                cost_keeping < cost_changing):
                            to_node_keeping.available_sim_interval = _MAX_TIME_INTERVAL
                            current_node.available_sim_interval = t + safe_headway_in_simu_interval
                            agent.micro_path_node_seq_no_list.append(
                                to_node_keeping.node_seq_no)
                            agent.micro_path_link_seq_no_list.append(
                                outgoing_link_keeping.link_seq_no)
                            agent.micro_path_node_id_list.append(
                                to_node_keeping.node_id)
                            agent.micro_path_link_id_list.append(
                                outgoing_link_keeping.link_id)
                            agent.m_Veh_LinkArrivalTime_in_simu_interval.append(
                                t)
                            agent.m_Veh_LinkDepartureTime_in_simu_interval.append(
                                t + outgoing_link_keeping.
                                free_flow_travel_time_in_simu_interval)

                        elif (cost_changing < _MAX_TIME_INTERVAL) & (
                                cost_changing < cost_keeping):
                            to_node_changing.available_sim_interval = _MAX_TIME_INTERVAL
                            current_node.available_sim_interval = t + safe_headway_in_simu_interval
                            agent.micro_path_node_seq_no_list.append(
                                to_node_changing.node_seq_no)
                            agent.micro_path_link_seq_no_list.append(
                                outgoing_link_changing.link_seq_no)
                            agent.micro_path_node_id_list.append(
                                to_node_changing.node_id)
                            agent.micro_path_link_id_list.append(
                                outgoing_link_changing.link_id)
                            agent.m_Veh_LinkArrivalTime_in_simu_interval.append(
                                t)
                            agent.m_Veh_LinkDepartureTime_in_simu_interval.append(
                                t + outgoing_link_changing.
                                free_flow_travel_time_in_simu_interval)

                        elif (cost_keeping < _MAX_TIME_INTERVAL) & (
                                abs(cost_keeping - cost_changing) < 0.1):
                            if random.random() < 0.15:
                                to_node_changing.available_sim_interval = _MAX_TIME_INTERVAL
                                current_node.available_sim_interval = t + safe_headway_in_simu_interval
                                agent.micro_path_node_seq_no_list.append(
                                    to_node_changing.node_seq_no)
                                agent.micro_path_link_seq_no_list.append(
                                    outgoing_link_changing.link_seq_no)
                                agent.micro_path_node_id_list.append(
                                    to_node_changing.node_id)
                                agent.micro_path_link_id_list.append(
                                    outgoing_link_changing.link_id)
                                agent.m_Veh_LinkArrivalTime_in_simu_interval.append(
                                    t)
                                agent.m_Veh_LinkDepartureTime_in_simu_interval.append(
                                    t + outgoing_link_changing.
                                    free_flow_travel_time_in_simu_interval)
                            else:
                                to_node_keeping.available_sim_interval = _MAX_TIME_INTERVAL
                                current_node.available_sim_interval = t + safe_headway_in_simu_interval
                                agent.micro_path_node_seq_no_list.append(
                                    to_node_keeping.node_seq_no)
                                agent.micro_path_link_seq_no_list.append(
                                    outgoing_link_keeping.link_seq_no)
                                agent.micro_path_node_id_list.append(
                                    to_node_keeping.node_id)
                                agent.micro_path_link_id_list.append(
                                    outgoing_link_keeping.link_id)
                                agent.m_Veh_LinkArrivalTime_in_simu_interval.append(
                                    t)
                                agent.m_Veh_LinkDepartureTime_in_simu_interval.append(
                                    t + outgoing_link_keeping.
                                    free_flow_travel_time_in_simu_interval)

                        else:
                            agent.m_Veh_LinkDepartureTime_in_simu_interval[
                                -1] += 1

            for agent_no in agent_remove_list:
                active_agent_list.remove(agent_no)

        printProgress(self.Simulation_EndTimeInMin,
                      self.Simulation_EndTimeInMin,
                      width=30)
        print()
        time_end = time.time()
        print(
            f'time used for traffic simulation: {round(time_end - time_start, 3)} seconds'
        )
def train_T_iter(model, T, data):
    for step in range(T):
        x, y = next(data)
        train_loss = model.train_on_batch(x, y)
        util.printProgress(step, 0, train_loss)
    return model
Exemple #19
0
def main():
    ''' Main function'''
    parser = argparse.ArgumentParser(
        description=
        'Convert from datasets (COCO, COCO_YOLO, UA-DETRAC, MOT2020, Bdd100k, AIHub, CrowdHuman) to YOLO format.'
    )
    parser.add_argument('--imgDir',
                        type=str,
                        help='directory of images.',
                        default='./copy')
    parser.add_argument('--saveDir',
                        type=str,
                        help='directory of images.',
                        default='./augmentation')
    parser.add_argument('--manipastFile',
                        type=str,
                        help='manipast file',
                        default="manipast.txt")
    config = parser.parse_args()

    manipast = ""
    files = os.listdir(config.imgDir)
    if not os.path.exists(config.saveDir):
        os.makedirs(config.saveDir)

    progressCnt = 0
    printProgress(progressCnt, len(files), ' Data-augmentation Progress: ',
                  'Complete')

    BLACK = [0, 0, 0]
    for file in files:
        fileName, fextension = os.path.splitext(file)
        if fextension == '.jpg' or fextension == '.png':
            img = cv2.imread(os.path.join(config.imgDir, file),
                             cv2.IMREAD_COLOR)
            height = float(img.shape[0])
            width = float(img.shape[1])

            ratio = 1
            dx = int(width * ratio / 2.0)
            dy = int(height * ratio / 2.0)
            dst = cv2.copyMakeBorder(img,
                                     top=dy,
                                     bottom=dy,
                                     left=dx,
                                     right=dx,
                                     borderType=cv2.BORDER_CONSTANT,
                                     value=BLACK)

            lines = ''
            writeStr = ''
            writeStr2 = ''

            txtFile = fileName + '.txt'
            with open(os.path.join(config.imgDir, txtFile), 'r+') as f:
                lines = f.readlines()

            for j in range(len(lines)):
                label, cx, cy, w, h = lines[j].split(' ')[0:5]
                cx, cy, w, h = float(cx), float(cy), float(w), float(h)

                if min(w, h) > 0.015 or max(w, h) > 0.03:
                    writeStr += "{} {} {} {} {}\n".format(label, cx, cy, w, h)

                labelWidth = width * w
                labelHeight = height * h
                labelCx = width * cx
                labelCy = height * cy

                newCx = (dx + labelCx) / (width + (width * ratio))
                newCy = (dy + labelCy) / (height + (height * ratio))
                newW = (w / (1 + ratio))
                newH = (h / (1 + ratio))
                if min(newW, newH) > 0.015 or max(newW, newH) > 0.03:
                    writeStr2 += "{} {} {} {} {}\n".format(
                        label, newCx, newCy, newW, newH)

            if writeStr:
                with open(os.path.join(config.saveDir, txtFile), 'w+') as f:
                    f.write(writeStr)
            if writeStr2:
                with open(
                        os.path.join(config.saveDir, fileName + '-border.txt'),
                        'w+') as f:
                    f.write(writeStr2)
                cv2.imwrite(
                    os.path.join(config.saveDir,
                                 fileName + '-border' + fextension), dst)
                manipast += os.path.join(
                    config.saveDir, fileName + '-border' + fextension) + '\n'

        progressCnt += 1
        printProgress(progressCnt, len(files), ' Data-augmentation Progress: ',
                      'Complete')

    with open(os.path.join(config.saveDir, config.manipastFile),
              "a+") as manipastFile:
        manipastFile.write(manipast)
Exemple #20
0
def finetune(model_name, save_dir, gpu_id, vid_i, k, T, iter_num):
    params = param.get_general_params()
    img_width = params['IMG_WIDTH']
    img_height = params['IMG_HEIGHT']

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    vgg_model = truncated_vgg.vgg_norm()
    networks.make_trainable(vgg_model, False)
    response_weights = sio.loadmat(
        '../data/vgg_activation_distribution_train.mat')
    model = networks.network_posewarp(params)
    weight_path = str(
        os.path.join(
            params['model_save_dir'],
            os.path.join(f"{model_name}",
                         f'{iter_num}.h5')))  # model name doesn't super work
    model.load_weights(weight_path)
    model.compile(optimizer=Adam(lr=1e-4),
                  loss=[networks.vgg_loss(vgg_model, response_weights, 12)])

    # train for T iterations
    train_feed = data_generation.create_feed(
        params,
        k,
        ModelMode.finetune,
        vid_i=vid_i,
        txtfile=f'../testset_5_v3/train_{vid_i}_img.txt')
    for step in range(T):
        x, y = next(train_feed)
        train_loss = model.train_on_batch(x, y)
        util.printProgress(step, 0, train_loss)

    # test on all items
    test_feed, dir_len = data_generation.create_test_feed(
        params,
        k,
        vid_i=vid_i,
        txtfile=f'../testset_5_v3/test_{vid_i}_img.txt',
        k_txtfile=f'../testset_5_v3/train_{vid_i}_img.txt')
    scores = np.zeros((dir_len, 3))
    for j in range(dir_len):
        try:
            x, y, scale, pos, img_num, src = next(test_feed)
            arr_loss = model.predict_on_batch(x)
        except cv2.error as e:
            print("OpenCV Error, gonna ignore")
            continue
        i = 0
        generated = (arr_loss[i] + 1) * 128
        gen_resized = data_generation.reverse_center_and_scale_image(
            generated, img_width, img_height, pos, scale)
        target = (y[i] + 1) * 128
        target_resized = data_generation.reverse_center_and_scale_image(
            target, img_width, img_height, pos, scale)
        source = (x[0][i] + 1) * 128
        # resized_source = cv2.resize(source, (0, 0), fx=2, fy=2)
        # source_resized = data_generation.reverse_center_and_scale_image(source, img_width, img_height, pos, scale)
        modified_img = data_generation.add_source_to_image(gen_resized, src)
        cv2.imwrite(save_dir + f'/{img_num:08d}.png', gen_resized)
        scores[j][0] = compare_ssim(gen_resized,
                                    target_resized,
                                    multichannel=True,
                                    data_range=256)
        scores[j][1] = compare_psnr(gen_resized,
                                    target_resized,
                                    data_range=256)
        scores[j][2] = compare_mse(gen_resized, target_resized)

    mean_scores = scores.mean(axis=0)
    std_scores = scores.std(axis=0)

    print(mean_scores)
    print(std_scores)
    save_dict = os.path.join(save_dir, f"saved_scores_{vid_i}.pkl")
    pickle.dump(scores, open(save_dict, "wb"))
Exemple #21
0
    def parse(self):
        data = {}
        files = os.listdir(self.config.datasetsDir)

        # Fine the annotation files.
        labelFiles = []
        for file in files:
            if file.split('.')[-1] == 'odgt':
                labelFiles.append(file)

        # Move the images from Images__train#num to Imgaes__train.
        if os.listdir(self.trainImgDir) == 0:
            for trainFile in ["Images__train01", "Images__train02", "Images__train03"]:
                currentDir = os.path.join(self.config.datasetsDir, trainFile)
                files = os.listdir(currentDir)

                self.progressCnt = 0
                self.length = len(files)
                printProgress(self.progressCnt, self.length, ' CrowdHuman {} moving Progress: '.format(trainFile), 'Complete')
                for file in files:
                    shutil.move(os.path.join(currentDir, file), self.trainImgDir)
                    self.progressCnt += 1
                    printProgress(self.progressCnt, self.length, ' CrowdHuman {} moving Progress: '.format(trainFile), 'Complete')
        else:
            print("[Info] {} has {} images ".format(self.trainImgDir, len(os.listdir(self.trainImgDir))))
        for labelFile in labelFiles:
            if labelFile.find('train') != -1:
                imgDir = self.trainImgDir
                type = 'train'
            else:
                imgDir = self.valImgDir
                type = 'valid'

            with open(os.path.join(self.config.datasetsDir, labelFile)) as yamlFile:

                print("[info] file ", os.path.join(self.config.datasetsDir, labelFile))

                self.progressCnt = 0
                self.length = len(os.listdir(imgDir))
                printProgress(self.progressCnt, self.length, (' CrowdHuman ' + type + ' Parsing Progress: '), 'Complete')
                for yamlData in yamlFile:
                    image = yaml.load(yamlData)
                    filename = image["ID"] + '.jpg'
                    img = cv2.imread(os.path.join(imgDir, filename))
                    size = {
                        "width": float(img.shape[1]),
                        "height": float(img.shape[0])
                    }
                    info = {
                        "path": imgDir,
                        "type":type,
                        "ignore": None
                    }
                    for label in image['gtboxes']:
                        if 'extra' in label and 'ignore' in label['extra'] and label['extra']['ignore'] == 1:
                            continue
                        if 'extra' in label and 'unsure' in label['extra'] and label['extra']['unsure'] == 1:
                            continue
                        bbox = {
                        "xmin": float(label['fbox'][0]),
                        "ymin": float(label['fbox'][1]),
                        "xmax": float(label['fbox'][0]) + float(label['fbox'][2]),
                        "ymax": float(label['fbox'][1]) + float(label['fbox'][3])
                        }
                        bboxInfo = {
                            "name": 'person',
                            "bbox": bbox
                        }

                        if filename in data:
                            obj_idx = data[filename]["objects"]["num_obj"] + 1
                            data[filename]["objects"][str(obj_idx)] = bboxInfo
                            data[filename]["objects"]["num_obj"] += 1
                        else:
                            obj = {
                                "num_obj": 1,
                                "1": bboxInfo
                            }
                            data[filename] = {
                                "info": info,
                                "size": size,
                                "objects": obj
                            }
                        bbox = {}
                        bboxInfo = {}

                    self.progressCnt += 1
                    printProgress(self.progressCnt, self.length, (' CrowdHuman ' + type + ' Parsing Progress: '), 'Complete')

        return data
Exemple #22
0
def train(model_name, gpu_id):

    params = param.getGeneralParams()
    gpu = '/gpu:' + str(gpu_id)

    network_dir = params['project_dir'] + '/results/networks/' + model_name

    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    train_feed = datageneration.createFeed(params, "train_vids.txt", 50000)
    test_feed = datageneration.createFeed(params, "test_vids.txt", 5000)

    batch_size = params['batch_size']

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True
    set_session(tf.Session(config=config))

    gan_lr = 5e-5
    disc_lr = 5e-5
    disc_loss = 0.1

    vgg_model_num = 184000

    with tf.device(gpu):
        vgg_model = myVGG.vgg_norm()
        networks.make_trainable(vgg_model, False)
        response_weights = sio.loadmat('mean_response.mat')
        generator = networks.network_fgbg(params, vgg_model, response_weights)
        generator.load_weights('../results/networks/fgbg_vgg_new/' +
                               str(vgg_model_num) + '.h5')

        discriminator = networks.discriminator(params)
        discriminator.compile(loss=networks.wass, optimizer=RMSprop(disc_lr))
        gan = networks.gan(generator, discriminator, params, vgg_model,
                           response_weights, disc_loss, gan_lr)

    for step in xrange(vgg_model_num + 1, vgg_model_num + 5001):
        for j in xrange(2):
            for l in discriminator.layers:
                weights = l.get_weights()
                weights = [np.clip(w, -0.01, 0.01) for w in weights]
                l.set_weights(weights)

            X, Y = next(train_feed)

            with tf.device(gpu):
                gen = generator.predict(X)

            #Train discriminator
            networks.make_trainable(discriminator, True)

            X_tgt_img_disc = np.concatenate((Y, gen))
            X_src_pose_disc = np.concatenate((X[1], X[1]))
            X_tgt_pose_disc = np.concatenate((X[2], X[2]))

            L = np.ones(2 * batch_size)
            L[0:batch_size] = -1

            inputs = [X_tgt_img_disc, X_src_pose_disc, X_tgt_pose_disc]
            d_loss = discriminator.train_on_batch(inputs, L)
            networks.make_trainable(discriminator, False)

        #TRAIN GAN
        L = -1 * np.ones(batch_size)
        X, Y = next(train_feed)
        g_loss = gan.train_on_batch(X, [Y, L])
        util.printProgress(step, 0, [g_loss[1], d_loss])

        if (step % params['model_save_interval'] == 0):
            gan.save(network_dir + '/' + str(step) + '.h5')
        '''
Exemple #23
0
    def parse(self):
        data = {}
        for labelFile in self.labelFiles:
            if labelFile.find('train') != -1:
                imgDir = self.trainImgDir
                type = 'train'
            else:
                imgDir = self.valImgDir
                type = 'valid'

            self.progressCnt = 0
            labels = os.listdir(labelFile)
            length = len(labels)
            printProgress(self.progressCnt, length, ' VisDrone Parsing Progress {}: '.format(type), 'Complete')

            for label in labels:
                fName, fextension = os.path.splitext(label)
                filename = fName + ".jpg"
                img = cv2.imread(os.path.join(imgDir, fName + ".jpg"))
                imgHeight, imgWidth, _ = img.shape
                size = {
                    "width": imgWidth,
                    "height": imgHeight
                }
                ignored_boxes = []
                with open(os.path.join(labelFile, label), 'r') as f:
                    lines = f.readlines()
                    for line in lines:
                        line = line.split(',')
                        if line[5] == "0":
                            bbox = {
                                "xmin": int(line[0]),
                                "ymin": int(line[1]),
                                "xmax": (int(line[2]) + int(line[0])),
                                "ymax": (int(line[3]) + int(line[1]))
                            }
                            ignored_boxes.append(bbox)
                    info = {
                        "path": imgDir,
                        "type": type,
                        "ignore": ignored_boxes
                    }
                    for line in lines:
                        line = line.split(',')
                        if(line[5] == "0"):
                            continue
                        bbox = {
                                "xmin": int(line[0]),
                                "ymin": int(line[1]),
                                "xmax": (int(line[2]) + int(line[0])),
                                "ymax": (int(line[3]) + int(line[1]))
                        }
                        bboxInfo = {
                            "name": self.objects[int(line[5])], # line[5] means classID
                            "bbox": bbox
                        }

                        if filename in data:
                            obj_idx = data[filename]["objects"]["num_obj"] + 1
                            data[filename]["objects"][str(obj_idx)] = bboxInfo
                            data[filename]["objects"]["num_obj"] += 1
                        else:
                            obj = {
                                "num_obj": 1,
                                "1": bboxInfo
                            }
                            data[filename] = {
                                "info": info,
                                "size": size,
                                "objects": obj
                            }

                self.progressCnt += 1
                printProgress(self.progressCnt, length, ' VisDrone Parsing Progress: {}'.format(type), 'Complete')
        return data
Exemple #24
0
    def parse(self):
        data = {}
        for labelFile in self.labelFiles:
            if labelFile.find('train') != -1:
                imgDir = self.trainImgDir
                type = 'train'
            else:
                imgDir = self.valImgDir
                type = 'valid'

            json_data = json.load(open(labelFile))
            images_info = json_data["images"]
            cls_info = json_data["categories"]
            self.progressCnt = 0
            length = len(json_data["annotations"])
            printProgress(self.progressCnt, length, ' COCO Parsing Progress {}: '.format(type), 'Complete')

            info = {
                "path": imgDir,
                "type": type,
                "ignore": None
            }

            for anno in json_data["annotations"]:
                imageId = anno["image_id"]
                clsId = anno["category_id"]

                for img_info in images_info:
                    if img_info["id"] == imageId:
                        filename, imgWidth, imgHeight = img_info["file_name"], img_info["width"], img_info["height"]

                for category in cls_info:
                    if category["id"] == clsId:
                        cls = category["name"]

                size = {
                    "width": imgWidth,
                    "height": imgHeight
                }

                bbox = {
                    "xmin": anno["bbox"][0],
                    "ymin": anno["bbox"][1],
                    "xmax": anno["bbox"][2] + anno["bbox"][0],
                    "ymax": anno["bbox"][3] + anno["bbox"][1]
                }

                bboxInfo = {
                    "name": cls,
                    "bbox": bbox
                }

                if filename in data:
                    obj_idx = data[filename]["objects"]["num_obj"] + 1
                    data[filename]["objects"][str(obj_idx)] = bboxInfo
                    data[filename]["objects"]["num_obj"] += 1
                else:
                    obj = {
                        "num_obj": 1,
                        "1": bboxInfo
                    }
                    data[filename] = {
                        "info": info,
                        "size": size,
                        "objects": obj
                    }

                self.progressCnt += 1
                printProgress(self.progressCnt, length, ' COCO Parsing Progress: {}'.format(type), 'Complete')
        return data
Exemple #25
0
def train(model_name, gpu_id):

    params = param.getGeneralParams()
    gpu = '/gpu:' + str(gpu_id)

    network_dir = params['project_dir'] + '/results/networks/' + model_name

    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    train_feed = datageneration.createFeed(params, "train_vids.txt")
    test_feed = datageneration.createFeed(params, "test_vids.txt")

    batch_size = params['batch_size']

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True

    with tf.Session(config=config) as sess:

        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        gan_lr = 1e-4
        disc_lr = 1e-4
        disc_loss = 0.1

        with tf.device(gpu):
            vgg_model = myVGG.vgg_norm()
            networks.make_trainable(vgg_model, False)
            response_weights = sio.loadmat('mean_response.mat')
            #generator = networks.network_pix2pix(params,vgg_model,response_weights)
            generator = networks.network_fgbg(params)
            generator.load_weights('../results/networks/fgbg_vgg/100000.h5')

            discriminator = networks.discriminator(params)
            discriminator.compile(loss='binary_crossentropy',
                                  optimizer=Adam(lr=disc_lr))
            gan = networks.gan(generator, discriminator, params, vgg_model,
                               response_weights, disc_loss, gan_lr)
            gan.compile(optimizer=Adam(lr=gan_lr),
                        loss=[
                            networks.vggLoss(vgg_model, response_weights),
                            'binary_crossentropy'
                        ],
                        loss_weights=[1.0, disc_loss])

        for step in xrange(10001):

            X, Y = next(train_feed)

            with tf.device(gpu):
                gen = generator.predict(X)  #[0:3])

            #Train discriminator
            X_tgt_img_disc = np.concatenate((Y, gen))
            X_src_pose_disc = np.concatenate((X[1], X[1]))
            X_tgt_pose_disc = np.concatenate((X[2], X[2]))

            L = np.zeros([2 * batch_size])
            L[0:batch_size] = 1

            inputs = [X_tgt_img_disc, X_src_pose_disc, X_tgt_pose_disc]
            d_loss = discriminator.train_on_batch(inputs, L)

            #Train the discriminator a couple of iterations before starting the gan
            if (step < 5):
                util.printProgress(step, 0, [0, d_loss])
                step += 1
                continue

            #TRAIN GAN
            L = np.ones([batch_size])
            X, Y = next(train_feed)
            g_loss = gan.train_on_batch(X, [Y, L])
            util.printProgress(step, 0, [g_loss[1], d_loss])
            '''
			#Test
			if(step % params['test_interval'] == 0):
				n_batches = 8
				test_loss = np.zeros(2)			
				for j in xrange(n_batches):	
					X,Y = next(warp_test_feed)
					#test_loss += np.array(generator.test_on_batch(X_warp,Y_warp))
					L = np.zeros([batch_size,2])
					L[:,1] = 1 #Fake images

					test_loss_j = gan_warp.test_on_batch(X_warp, [Y_warp,L])
					test_loss += np.array(test_loss_j[1:3])
	
				test_loss /= (n_batches)
				util.printProgress(step,1,test_loss)
			'''

            if (step % params['model_save_interval'] == 0 and step > 0):
                gan.save(network_dir + '/' + str(step) + '.h5')
Exemple #26
0
    def parse(self):
        data = {}
        labelFiles = os.listdir(self.labelDir)
        for labelFile in labelFiles:
            if labelFile.find('train') != -1:
                imgDir = self.trainImgDir
                type = 'train'
            else:
                imgDir = self.valImgDir
                type = 'valid'

            with open(os.path.join(self.labelDir, labelFile)) as jsonFile:
                jsonData = json.load(jsonFile)
                self.progressCnt = 0
                self.length = len(jsonData)
                printProgress(self.progressCnt, self.length, (' Bdd100k ' + type + ' Parsing Progress: '), 'Complete')
                for image in jsonData:
                    filename = image["name"]
                    size = {
                        "width": float(1280),
                        "height": float(720)
                    }
                    '''
                    from PIL import Image
                    img = Image.open(os.path.join(imgDir, filename))
                    size = {
                        "width": float(img.size[0]),
                        "height": float(img.size[1])
                    }
                    '''
                    info = {
                        "path": imgDir,
                        "type": type,
                        "ignore": None
                    }
                    for label in image['labels']:
                        if not 'box2d' in label:
                            continue
                        bbox = {
                        "xmin": float(label['box2d']['x1']),
                        "ymin": float(label['box2d']['y1']),
                        "xmax": float(label['box2d']['x2']),
                        "ymax": float(label['box2d']['y2'])
                        }
                        bboxInfo = {
                            "name": label['category'],
                            "bbox": bbox
                        }

                        if filename in data:
                            obj_idx = data[filename]["objects"]["num_obj"] + 1
                            data[filename]["objects"][str(obj_idx)] = bboxInfo
                            data[filename]["objects"]["num_obj"] += 1
                        else:
                            obj = {
                                "num_obj": 1,
                                "1": bboxInfo
                            }
                            data[filename] = {
                                "info": info,
                                "size": size,
                                "objects": obj
                            }
                        bbox = {}
                        bboxInfo = {}

                    self.progressCnt += 1
                    printProgress(self.progressCnt, self.length, (' Bdd100k ' + type + ' Parsing Progress: '), 'Complete')
        return data
Exemple #27
0
def test(model_name, gpu_id):
    params = param.get_general_params()
    network_dir = params['model_save_dir'] + '/' + model_name

    # if not os.path.isdir(network_dir):
    #     os.mkdir(network_dir)

    train_feed = data_generation.create_feed(params,
                                             params['data_dir'],
                                             'test',
                                             do_augment=False)

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True

    gan_lr = 1e-4
    disc_lr = 1e-4
    disc_loss = 0.1

    generator = networks.network_posewarp(params)
    # generator.load_weights('../models/vgg_100000.h5')
    generator.load_weights(
        '/versa/kangliwei/motion_transfer/posewarp-cvpr2018/models/0301_fullfinetune/9000.h5'
    )

    mask_delta_model = Model(input=generator.input,
                             output=generator.get_layer('mask_delta').output)
    src_mask_model = Model(input=generator.input,
                           output=generator.get_layer('mask_src').output)

    discriminator = networks.discriminator(params)
    discriminator.compile(loss='binary_crossentropy',
                          optimizer=Adam(lr=disc_lr))

    vgg_model = truncated_vgg.vgg_norm()
    networks.make_trainable(vgg_model, False)
    response_weights = sio.loadmat(
        '../data/vgg_activation_distribution_train.mat')

    gan = networks.gan(generator, discriminator, params)
    gan.compile(optimizer=Adam(lr=gan_lr),
                loss=[
                    networks.vgg_loss(vgg_model, response_weights, 12),
                    'binary_crossentropy'
                ],
                loss_weights=[1.0, disc_loss])

    n_iters = 10000
    batch_size = params['batch_size']

    for step in range(n_iters):

        x, y = next(train_feed)

        gen = generator.predict(x)

        src_mask_delta = mask_delta_model.predict(x)
        print('delta_max', src_mask_delta.max())
        src_mask_delta = src_mask_delta * 255
        src_mask = src_mask_model.predict(x)
        print('mask_max', src_mask.max())
        src_mask = src_mask * 255
        # print('src_mask_delta', type(src_mask_delta), src_mask_delta.shape)

        y = (y / 2 + 0.5) * 255.0
        gen = (gen / 2 + 0.5) * 255.0
        for i in range(gen.shape[0]):  # iterate in batch
            cv2.imwrite('pics/src' + str(i) + '.jpg', x[0][i] * 255)
            cv2.imwrite('pics/gen' + str(i) + '.jpg', gen[i])
            cv2.imwrite('pics/y' + str(i) + '.jpg', y[i])
            for j in range(11):
                cv2.imwrite('pics/seg_delta_' + str(i) + '_' + str(j) + '.jpg',
                            src_mask_delta[i][:, :, j])
            for j in range(11):
                cv2.imwrite('pics/seg_' + str(i) + '_' + str(j) + '.jpg',
                            src_mask[i][:, :, j])
        break

        # Train discriminator
        x_tgt_img_disc = np.concatenate((y, gen))
        x_src_pose_disc = np.concatenate((x[1], x[1]))
        x_tgt_pose_disc = np.concatenate((x[2], x[2]))

        L = np.zeros([2 * batch_size])
        L[0:batch_size] = 1

        inputs = [x_tgt_img_disc, x_src_pose_disc, x_tgt_pose_disc]
        d_loss = discriminator.train_on_batch(inputs, L)

        # Train the discriminator a couple of iterations before starting the gan
        if step < 5:
            util.printProgress(step, 0, [0, d_loss])
            step += 1
            continue

        # TRAIN GAN
        L = np.ones([batch_size])
        x, y = next(train_feed)
        g_loss = gan.train_on_batch(x, [y, L])
        util.printProgress(step, 0, [g_loss[1], d_loss])

        if step % params['model_save_interval'] == 0 and step > 0:
            generator.save(network_dir + '/' + str(step) + '.h5')
Exemple #28
0
def train(model_name, gpu_id):
    params = param.get_general_params()
    network_dir = params['model_save_dir'] + '/' + model_name

    if not os.path.isdir(network_dir):
        os.mkdir(network_dir)

    train_feed = data_generation.create_feed(params, params['data_dir'], 'train')

    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.allow_soft_placement = True

    gan_lr = 1e-4
    disc_lr = 1e-4
    disc_loss = 0.1

    generator = networks.network_posewarp(params)
    generator.load_weights('../models/vgg_100000.h5')

    discriminator = networks.discriminator(params)
    discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=disc_lr))

    vgg_model = truncated_vgg.vgg_norm()
    networks.make_trainable(vgg_model, False)
    response_weights = sio.loadmat('../data/vgg_activation_distribution_train.mat')

    gan = networks.gan(generator, discriminator, params)
    gan.compile(optimizer=Adam(lr=gan_lr),
                loss=[networks.vgg_loss(vgg_model, response_weights, 12), 'binary_crossentropy'],
                loss_weights=[1.0, disc_loss])

    n_iters = 10000
    batch_size = params['batch_size']

    for step in range(n_iters):

        x, y = next(train_feed)

        gen = generator.predict(x)

        # Train discriminator
        x_tgt_img_disc = np.concatenate((y, gen))
        x_src_pose_disc = np.concatenate((x[1], x[1]))
        x_tgt_pose_disc = np.concatenate((x[2], x[2]))

        L = np.zeros([2 * batch_size])
        L[0:batch_size] = 1

        inputs = [x_tgt_img_disc, x_src_pose_disc, x_tgt_pose_disc]
        d_loss = discriminator.train_on_batch(inputs, L)

        # Train the discriminator a couple of iterations before starting the gan
        if step < 5:
            util.printProgress(step, 0, [0, d_loss])
            step += 1
            continue

        # TRAIN GAN
        L = np.ones([batch_size])
        x, y = next(train_feed)
        g_loss = gan.train_on_batch(x, [y, L])
        util.printProgress(step, 0, [g_loss[1], d_loss])

        if step % params['model_save_interval'] == 0 and step > 0:
            gan.save(network_dir + '/' + str(step) + '.h5')
def train(model_name, gpu_id):

    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True
    tf_config.allow_soft_placement = True

    with tf.Session(config=tf_config) as sess:

        params = param.get_general_params()
        network_dir = params['model_save_dir'] + '/' + model_name

        if not os.path.isdir(network_dir):
            os.mkdir(network_dir)

        train_feed = data_generation.create_feed(params, params['data_dir'], "train")
        # test_feed = data_generation.create_feed(params,  params['data_dir'], "test")

        os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)

        gan_lr  = 1e-3
        disc_lr = 1e-3
        disc_loss = 0.1

        generator = networks.network_posewarp(params)
        # generator.load_weights('../models/posewarp_vgg/100000.h5')

        discriminator = networks.discriminator(params)
        discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=disc_lr))

        vgg_model = truncated_vgg.vgg_norm()
        networks.make_trainable(vgg_model, False)
        response_weights = sio.loadmat('../Models/vgg_activation_distribution_train.mat')

        gan = networks.gan(generator, discriminator, params)

        gan.compile(optimizer=Adam(lr=gan_lr),
                    loss=[networks.vgg_loss(vgg_model, response_weights, 12), 'binary_crossentropy'],
                    loss_weights=[1.0, disc_loss])

        n_iters = params['n_training_iter']
        batch_size = params['batch_size']

        summary_writer = tf.summary.FileWriter("./logs", graph=sess.graph)

        tr_x, tr_y = next(train_feed)
        # te_x, te_y = next(test_feed)

        # Prepare output directories if they don't exist.
        output_dir = '../Output/' + model_name + '/'

        if not os.path.isdir(output_dir):
            os.mkdir(output_dir)

        scipy.misc.imsave(output_dir + 'tr_orig_image.png', tr_x[0][0, :, :, :])
        scipy.misc.imsave(output_dir + 'tr_targ_image.png', tr_y[0, :, :, :])
        # scipy.misc.imsave(output_dir + 'te_orig_image.png', te_x[0][0, :, :, :])
        # scipy.misc.imsave(output_dir + 'te_targ_image.png', te_y[0, :, :, :])

        print("Batch size: " + str(batch_size))

        for step in range(n_iters):

            x, y = next(train_feed)

            gen = generator.predict(x)

            # Train discriminator
            x_tgt_img_disc  = np.concatenate((y, gen))
            x_src_pose_disc = np.concatenate((x[1], x[1]))
            x_tgt_pose_disc = np.concatenate((x[2], x[2]))

            L = np.zeros([2 * batch_size])
            L[0:batch_size] = 1

            inputs = [x_tgt_img_disc, x_src_pose_disc, x_tgt_pose_disc]
            d_loss = discriminator.train_on_batch(inputs, L)

            # Train the discriminator a couple of iterations before starting the gan
            if step < 5:
                util.printProgress(step, 0, [0, d_loss])
                step += 1
                continue

            # TRAIN GAN
            L = np.ones([batch_size])
            x, y = next(train_feed)
            g_loss = gan.train_on_batch(x, [y, L])
            util.printProgress(step, 0, [g_loss[1], d_loss])

            if step % params['test_interval'] == 0:

                print(gen[0])

                gen = tf.get_default_graph().get_tensor_by_name("model_1/add_2_1/add:0")
                inp = tf.get_default_graph().get_tensor_by_name("in_img0:0")
                out = tf.get_default_graph().get_tensor_by_name("in_img1:0")
                p_s = tf.get_default_graph().get_tensor_by_name("mask_src/truediv:0")
                # p_t = tf.get_default_graph().get_tensor_by_name("in_pose1:0")


                image_summary_1 = tf.summary.image('images', [inp[0, :, :, :], out[0, :, :, :], gen[0, :, :, :]], max_outputs=100)
                # image_summary_2 = tf.summary.image('pose', [tf.reduce_sum(p_s[0, :, :, :], 2, keepdims=True)], max_outputs=100)

                image_summary_1 = sess.run(image_summary_1,feed_dict={"in_img0:0": x[0], "in_pose0:0": x[1], "in_pose1:0": x[2],
                                                                      "mask_prior:0": x[3], "trans_in:0": x[4], "in_img1:0": y,
                                                                      "input_3:0": x[0], "input_4:0": x[1], "input_5:0": x[2],
                                                                      "input_6:0": x[3], "input_7:0": x[4]})
                #
                # img_gen =  sess.run(image_summary_1,feed_dict={"in_img0:0": x[0], "in_pose0:0": x[1], "in_pose1:0": x[2],
                #                                                "mask_prior:0": x[3], "trans_in:0": x[4], "in_img1:0": y,
                #                                                "input_3:0": x[0], "input_4:0": x[1], "input_5:0": x[2],
                #                                                "input_6:0": x[3], "input_7:0": x[4]})


                # image_summary_2 = sess.run(image_summary_2, feed_dict={"in_img0:0" : x[0], "in_pose0:0" : x[1], "in_pose1:0" : x[2],
                #                                                     "mask_prior:0" : x[3], "trans_in:0" : x[4], "in_img1:0"  : y})

                summary_writer.add_summary(image_summary_1)
                # summary_writer.add_summary(image_summary_2)

                train_image = sess.run(gen, feed_dict={"in_img0:0": tr_x[0], "in_pose0:0": tr_x[1], "in_pose1:0": tr_x[2],
                                                       "mask_prior:0": tr_x[3], "trans_in:0": tr_x[4], "in_img1:0": tr_y,
                                                       "input_3:0": tr_x[0], "input_4:0": tr_x[1], "input_5:0": tr_x[2],
                                                       "input_6:0": tr_x[3], "input_7:0": tr_x[4]})
                #
                # test_image = sess.run(gen, feed_dict={"in_img0:0": te_x[0], "in_pose0:0": te_x[1], "in_pose1:0": te_x[2],
                #                                       "mask_prior:0": te_x[3], "trans_in:0": te_x[4], "in_img1:0": te_y,
                #                                       "input_3:0": te_x[0], "input_4:0": te_x[1], "input_5:0": te_x[2],
                #                                       "input_6:0": te_x[3], "input_7:0": te_x[4]})


                scipy.misc.imsave(output_dir + 'tr' + str(step) + ".png", train_image[0, :, :, :])
                # scipy.misc.imsave(output_dir + 'te' + str(step) + ".png", test_image[0, :, :, :])

            if step % params['model_save_interval'] == 0 and step > 0:
                gan.save(network_dir + '/' + str(step) + '.h5')
def train(model_name, gpu_id):

    with tf.Session() as sess:

        params = param.get_general_params()

        network_dir = params['model_save_dir'] + '/' + model_name

        # Creates models directory if not exist.
        if not os.path.isdir(network_dir):
            os.mkdir(network_dir)

        train_feed = data_generation.create_feed(params, params['data_dir'],
                                                 'train')
        test_feed = data_generation.create_feed(params, params['data_dir'],
                                                'test')

        os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        set_session(tf.Session(config=config))

        vgg_model = truncated_vgg.vgg_norm()
        networks.make_trainable(vgg_model, False)
        response_weights = sio.loadmat(
            '../data/vgg_activation_distribution_train.mat')
        model = networks.network_posewarp(params)
        model.compile(
            optimizer=Adam(lr=1e-4),
            loss=[networks.vgg_loss(vgg_model, response_weights, 12)])

        n_iters = params['n_training_iter']

        summary_writer = tf.summary.FileWriter(
            "D:\Proyectos\JEJU2018\Code\posewarp-cvpr2018\code\logs",
            graph=sess.graph)

        tr_x, tr_y = next(train_feed)
        te_x, te_y = next(test_feed)

        # Prepare output directories if they don't exist.
        output_dir = '../output/' + model_name + '/'

        if not os.path.isdir(output_dir):
            os.mkdir(output_dir)

        scipy.misc.imsave('../output/tr_orig_image.png', tr_x[0][0, :, :, :])
        scipy.misc.imsave('../output/tr_targ_image.png', tr_y[0, :, :, :])
        scipy.misc.imsave('../output/te_orig_image.png', te_x[0][0, :, :, :])
        scipy.misc.imsave('../output/te_targ_image.png', te_y[0, :, :, :])

        for step in range(0, n_iters):
            x, y = next(train_feed)

            train_loss = model.train_on_batch(x, y)

            util.printProgress(step, 0, train_loss)

            # out = sess.run(conv, feed_dict={"input_1:0" : x[0]})
            # plt.matshow(out[0, :, :, 0])
            # plt.show()

            gen = tf.get_default_graph().get_tensor_by_name(
                "loss/add_2_loss/lambda_5/add:0")
            inp = tf.get_default_graph().get_tensor_by_name("in_img0:0")
            out = tf.get_default_graph().get_tensor_by_name("in_img1:0")
            p_s = tf.get_default_graph().get_tensor_by_name(
                "mask_src/truediv:0")
            # p_t = tf.get_default_graph().get_tensor_by_name("in_pose1:0")

            image_summary_1 = tf.summary.image(
                'images', [inp[0, :, :, :], out[0, :, :, :], gen[0, :, :, :]],
                max_outputs=100)
            # image_summary_2 = tf.summary.image('pose', [tf.reduce_sum(p_s[0, :, :, :], 2, keepdims=True)], max_outputs=100)

            image_summary_1 = sess.run(image_summary_1,
                                       feed_dict={
                                           "in_img0:0": x[0],
                                           "in_pose0:0": x[1],
                                           "in_pose1:0": x[2],
                                           "mask_prior:0": x[3],
                                           "trans_in:0": x[4],
                                           "in_img1:0": y
                                       })

            # image_summary_2 = sess.run(image_summary_2, feed_dict={"in_img0:0" : x[0], "in_pose0:0" : x[1], "in_pose1:0" : x[2],
            #                                                     "mask_prior:0" : x[3], "trans_in:0" : x[4], "in_img1:0" : y})

            summary_writer.add_summary(image_summary_1)
            # summary_writer.add_summary(image_summary_2)

            train_image = sess.run(gen,
                                   feed_dict={
                                       "in_img0:0": tr_x[0],
                                       "in_pose0:0": tr_x[1],
                                       "in_pose1:0": tr_x[2],
                                       "mask_prior:0": tr_x[3],
                                       "trans_in:0": tr_x[4],
                                       "in_img1:0": tr_y
                                   })

            test_image = sess.run(gen,
                                  feed_dict={
                                      "in_img0:0": te_x[0],
                                      "in_pose0:0": te_x[1],
                                      "in_pose1:0": te_x[2],
                                      "mask_prior:0": te_x[3],
                                      "trans_in:0": te_x[4],
                                      "in_img1:0": te_y
                                  })
            if step > 0 and step % params['model_save_interval'] == 0:
                model.save_weights(network_dir + '/' + str(step) + '.h5')
Exemple #31
0
    def parse(self):
        data = {}
        ig_bboxes = []
        self.search(self.labelDir)
        self.genImgDirDict(self.imgRootDir)

        self.length = len(self.xmlFiles)
        printProgress(self.progressCnt, self.length, ' UA-Detrac Parsing Progress: ', 'Complete')
        size = {
            "width": float(960.),
            "height": float(540.),
        }
        for index, xml in enumerate(self.xmlFiles):
            ignored_boxes = []
            doc = ET.parse(xml)
            root = doc.getroot()
            xmlName, _ = os.path.splitext(xml.split('/')[-1])
            for mask in root.iter('ignored_region'):
                for box in mask.findall('box'):
                    ignored_box = {
                        "xmin": float(box.attrib['left']),
                        "ymin": float(box.attrib['top']),
                        "xmax": float(box.attrib['left']) + float(box.attrib['width']),
                        "ymax": float(box.attrib['top']) + float(box.attrib['height'])
                    }
                    ignored_boxes.append(ignored_box)

            for img in root.iter('frame'):

                imgId = img.attrib['num']
                # filename = os.path.join(self.imgDir[index],('img' + ('%05d' % int(img.attrib['num'])) + '.jpg')) # '%03d' % 1          # '001'

                filename = xmlName + ('-img' + ('%05d' % int(img.attrib['num'])) + '.jpg') # '%03d' % 1          # '001'
                type = 'valid' if random.randrange(0, 10) == 0 else 'train'

                info = {
                    "path": self.imgDir[filename],
                    "type": type,
                    "ignore": ignored_boxes
                }
                for target in img.findall('target_list/target'):
                    # target[0] : box
                    # target[1] : attribute
                    bbox = {
                        "xmin": float(target[0].attrib['left']),
                        "ymin": float(target[0].attrib['top']),
                        "xmax": float(target[0].attrib['left']) + float(target[0].attrib['width']),
                        "ymax": float(target[0].attrib['top']) + float(target[0].attrib['height'])
                    }
                        #print(box.tag, box.attrib)
                    bboxInfo = {
                        "name": target[1].attrib['vehicle_type'],
                        "bbox": bbox
                    }

                    if filename in data:
                        obj_idx = data[filename]["objects"]["num_obj"] + 1
                        data[filename]["objects"][str(obj_idx)] = bboxInfo
                        data[filename]["objects"]["num_obj"] += 1
                    else:
                        obj = {
                            "num_obj": 1,
                            "1": bboxInfo
                        }
                        data[filename] = {
                            "info": info,
                            "size": size,
                            "objects": obj
                        }
                    bbox = {}
                    bboxInfo = {}

            self.progressCnt += 1
            printProgress(self.progressCnt, self.length, ' UA-Detrac Parsing Progress: ', 'Complete')
        return data