Beispiel #1
0
    def summarize(self, basename, subtype = ''):
        """ Strip JS of most of its fields so that we have shorter load times
        on JS structures.
        
        Usage: dataset.summarize(basename)

        Input:
            basename - file base name. We assume we can generate a full name
            with dataset.getName(basename, 'output')

        Output:
            -NONE- but file will be saved in getName 'summary' format.
        """
     
        input  = self.getName(basename, type = 'output', subtype = subtype)
        output = self.getName(basename, type = 'summary', subtype = subtype)

        JS = self.loadData(basename, 'output', subtype = subtype)
        
        # Strip JS off of all unnecessary information
        JS.update()
        JS.Regions = None
        JS.Segmentations = None
        JS.Superpix = None
        JS.NmsRegions = None

        for r in JS.CompactReg:
            r.Children = None
            r.Siblings = None
            r.VisIdxImg = None
            r.Better_R = None
            r.Scores = None

        # Save output
        utils.save(output, {'JS': JS}, zipped=False)
Beispiel #2
0
    def save(self, filename='index', metadata=True, resources=True):
        ''' save metadata and content

        filename - defines just filename for three files: 
            - <filename>.html 
            - <filename>.metadata 
            - <filename>.resources

        Only the first one is HTML file, the rest of files are JSON files

        metadata - if True, HTTP response information will be stored into .metadata file
        resources - If True, resources metadata will be stores into .resources file 
        '''
        if not self.path:
            raise RuntimeError('Error! The path for storing content is not defined')

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

        # save content metadata
        if metadata:
            utils.save(os.path.join(self.path, '%s.metadata' % filename), 
                    content=unicode(json.dumps(self.metadata['headers'], indent=4, sort_keys=True)))

        # save resources metadata
        if resources and self.metadata['resources']:
            utils.save(os.path.join(self.path, '%s.resources' % filename), 
                    content=unicode(json.dumps(self.metadata['resources'], indent=4, sort_keys=True)))

        # save content
        offline_content = copy.deepcopy(self.content)
        offline_links = dict([(url, self.metadata['resources'][url]['filename']) for url in self.metadata['resources']])
        offline_content.make_links_offline(offline_links=offline_links)
        offline_content.save(os.path.join(self.path, '%s.html' % filename))
Beispiel #3
0
def downloadTests(url):
    tests = getTests(url)
    print("Processing Tests")
    os.mkdir("tests")
    for v in tests:
        print("Saving %s" % v["Próf"][0])
        save("tests", v["Próf"][1])
Beispiel #4
0
def plot_reconstruction_result(res):
    """ Plot original and reconstructed graph plus time series
    """
    fig = plt.figure(figsize=(32, 8))
    gs = mpl.gridspec.GridSpec(1, 4)

    # original graph
    orig_ax = plt.subplot(gs[0])
    plot_graph(nx.from_numpy_matrix(res.A.orig), orig_ax)
    orig_ax.set_title('Original graph')

    # time series
    ax = plt.subplot(gs[1:3])
    sns.tsplot(
        time='time', value='theta',
        unit='source', condition='oscillator',
        estimator=np.mean, legend=False,
        data=compute_solutions(res),
        ax=ax)
    ax.set_title(r'$A_{{err}} = {:.2}, B_{{err}} = {:.2}$'.format(*compute_error(res)))

    # reconstructed graph
    rec_ax = plt.subplot(gs[3])
    tmp = res.A.rec
    tmp[abs(tmp) < 1e-1] = 0
    plot_graph(nx.from_numpy_matrix(tmp), rec_ax)
    rec_ax.set_title('Reconstructed graph')

    plt.tight_layout()
    save(fig, 'reconstruction_overview')
Beispiel #5
0
def update_targets(week):
    target_week_data = "data/" + ("week_%s/" % week) + "target_users.pkl"
    invalid_week_data = "data/" + ("week_%s/" % week) + "invalid_users.pkl"
    update_info = {}
    invalid_users = []
    with open("data/target_users.pkl") as orig:
        targets = cPickle.load(orig)
        # for index, t in enumerate(targets, start=1):
            # print "--- get user=%s(%d:%d) ---" % (t, index, len(targets))
            # result = get_user_info(t)
            # if result is None:
                # print "--- user=%s get invalid info ---" % t
                # invalid_users.append(t)

            # update_info[t] = result
        from utils import iter_pool_do
        info_iter = iter_pool_do(get_user_info, targets.keys())
        index = 1
        for name, info in info_iter:
            print "--- get user=%s(%d:%d) ---" % (name, index, len(targets))
            if info is None:
                print "--- user=%s get invalid info ---" % name
                invalid_users.append(name)

            update_info[name] = info
            index += 1

    print "---- update to file %s %d targets---" % (
        target_week_data, len(update_info))
    save(target_week_data, update_info)
    print "---- update to file %s %d invalids---" % (
        invalid_week_data, len(invalid_users))
    save(invalid_week_data, invalid_users)
    return update_info
Beispiel #6
0
def test():
    R = Ret()
    prefix = '/home/mfkiller/stitch/tools/test/data/'
    R.prepare(prefix+'blocks/4.pkl',prefix+'imgs/',prefix+'cameralist.pkl',
	      prefix+'plane.pkl')
    #R.scale = 10.0
    utils.save(R, '../ret.pkl')
Beispiel #7
0
    def kinfu(self, basename, type = 'list_ext'):
        """ Use Kinect fusion on list of depth images, and store cloud

        Usage: kinfu(basename, type = 'list_ext')

        Input:
            basename - Path to file that contains depth image names.
            type{'list_ext'} - List file type ('list' or 'list_ext')
            
        Output:
            cloud, poses - Filenames to output cloud and poses from kinfu 

        """ 
        roslib.load_manifest('kinfu')
        import kinfu

        depth_textfile = self.getName(basename, type)

        # Need to read list and export using full names
        fullnames = list()
        with open(depth_textfile, 'rt') as file_:
            for name in file_:
                fullnames.append( self.getName(name.rstrip(), 'depth') )
                
        output_prefix = os.path.join(self.getPath(type), basename) + '_'

        KF = kinfu.KinFuPy('file')
        cloud, poses = KF.kinfu_list(fullnames, output_prefix, opts='singlepass')

        # Store cloud and poses
        utils.save(self.getName(basename, 'cloud'), {'cloud': cloud}, \
                   zipped=True)
        utils.save(self.getName(basename, 'poses'), {'poses': poses}, \
                   zipped=True)
        return cloud, poses
Beispiel #8
0
    def fit(self, trX, trY, batch_size=64, n_epochs=1, len_filter=LenFilter(), snapshot_freq=1, path=None, callback=None, callback_freq=1):
        """Train model on given training examples and return the list of costs after each minibatch is processed.

        Args:
          trX (list) -- Inputs
          trY (list) -- Outputs
          batch_size (int, optional) -- number of examples in a minibatch (default 64)
          n_epochs (int, optional)  -- number of epochs to train for (default 1)
          len_filter (object, optional) -- object to filter training example by length (default LenFilter())
          snapshot_freq (int, optional) -- number of epochs between saving model snapshots (default 1)
          path (str, optional) -- prefix of path where model snapshots are saved.
            If None, no snapshots are saved (default None)

        Returns:
          list -- costs of model after processing each minibatch
        """
        if len_filter is not None:
            trX, trY = len_filter.filter(trX, trY)
        trY = standardize_targets(trY, cost=self.cost)

        n = 0.
        stats = []
        t = time()
        costs = []
        for e in range(n_epochs):
            epoch_costs = []
            for xmb, ymb in self.iterator.iterXY(trX, trY):
                if not self.is_padseq_output:
                    c = self._train(xmb, ymb)
                else:
                    ymb, padsizes = ymb
                    c = self._train(xmb, ymb, padsizes)
                epoch_costs.append(c)
                n += len(ymb)
                if self.verbose >= 2:
                    n_per_sec = n / (time() - t)
                    n_left = len(trY) - n % len(trY)
                    time_left = n_left/n_per_sec
                    sys.stdout.write("\rEpoch %d Seen %d samples Avg cost %0.4f Time left %d seconds" % (e, n, np.mean(epoch_costs[-250:]), time_left))
                    sys.stdout.flush()
            costs.extend(epoch_costs)

            status = "Epoch %d Seen %d samples Avg cost %0.4f Time elapsed %d seconds" % (e, n, np.mean(epoch_costs[-250:]), time() - t)
            if self.verbose >= 2:
                sys.stdout.write("\r"+status) 
                sys.stdout.flush()
                sys.stdout.write("\n")
            elif self.verbose == 1:
                print status
            if path and e % snapshot_freq == 0:
                save(self, "{0}.{1}".format(path, e))
            if callback is not None and e % callback_freq == 0:
                try:
                    callback(e)
                except RNN.EarlyStoppingException:
                    break
        return costs
Beispiel #9
0
    def dump(self, filename):
        """ Dump model into file using pickle.
        
        Usage: model.dump(filename = 'FILENAME.pickle.zip')

        Input:
            filename - File to dump model into. Should end in '.pickle.zip'.
        """

        utils.save(filename, {'model': self}, zipped=True)
    def train(self, svm_kernel, codebook, des_option=constants.ORB_FEAT_OPTION, is_interactive=True):
        """
        Gets the descriptors for the training set and then calculates the SVM for them.

        Args:
            svm_kernel (constant): The kernel of the SVM that will be created.
            codebook (NumPy float matrix): Each row is a center of a codebook of Bag of Words approach.
            des_option (integer): The option of the feature that is going to be used as local descriptor.
            is_interactive (boolean): If it is the user can choose to load files or generate.

        Returns:
            cv2.SVM: The Support Vector Machine obtained in the training phase.
        """
        des_name = constants.ORB_FEAT_NAME if des_option == constants.ORB_FEAT_OPTION else constants.SIFT_FEAT_NAME
        k = len(codebook)
        x_filename = filenames.vlads_train(k, des_name)
        if is_interactive:
            data_option = input("Enter [1] to calculate VLAD vectors for the training set or [2] to load them.\n")
        else:
            data_option = constants.GENERATE_OPTION
        if data_option == constants.GENERATE_OPTION:
            # Getting the global vectors for all of the training set
            print("Getting global descriptors for the training set.")
            start = time.time()
            x, y = self.get_data_and_labels(self.dataset.get_train_set(), codebook, des_option)
            utils.save(x_filename, x)
            end = time.time()
            print("VLADs training vectors saved on file {0}".format(x_filename))
            self.log.train_vlad_time(end - start)
        else:
            # Loading the global vectors for all of the training set
            print("Loading global descriptors for the training set.")
            x = utils.load(x_filename)
            y = self.dataset.get_train_y()
            x = np.matrix(x, dtype=np.float32)
        svm = cv2.SVM()
        svm_filename = filenames.svm(k, des_name, svm_kernel)
        if is_interactive:
            svm_option = input("Enter [1] for generating a SVM or [2] to load one\n")
        else:
            svm_option = constants.GENERATE_OPTION
        if svm_option == constants.GENERATE_OPTION:
            # Calculating the Support Vector Machine for the training set
            print("Calculating the Support Vector Machine for the training set...")
            svm_params = dict(kernel_type=svm_kernel, svm_type=cv2.SVM_C_SVC, C=1)
            start = time.time()
            svm.train_auto(x, y, None, None, svm_params)
            end = time.time()
            self.log.svm_time(end - start)
            # Storing the SVM in a file
            svm.save(svm_filename)
        else:
            svm.load(svm_filename)
        return svm
Beispiel #11
0
def test():
    B0 = utils.load('blk/0.pkl')
    B1 = utils.load('blk/1.pkl')
    B2 = utils.load('blk/4.pkl')
    B3 = utils.load('blk/5.pkl')
    
    BM = [[B0,B1],[B2,B3]]
    P = Block()
    P.merge_from_matrix(BM)
    utils.save(P, 'mb.pkl')
    print P
Beispiel #12
0
def buildregion(args):
    """Given a region name and coordinates, build the corresponding region from tile chunks."""
    # this should work for single and multi threaded cases
    (log, name, regionx, regiony) = args
    log.log_debug(1,"Building region (%d,%d) of map %s..." % \
                  (regionx, regiony, name))
    yamlfile = file(os.path.join('Regions', name, 'Region.yaml'))
    myRegion = yaml.load(yamlfile)
    yamlfile.close()
    regionsize = 32 * 16
    tilexrange = xrange(regionx * (regionsize / myRegion.tilesize), \
                        (regionx + 1) * (regionsize / myRegion.tilesize))
    tileyrange = xrange(regiony * (regionsize / myRegion.tilesize), \
                        (regiony + 1) * (regionsize / myRegion.tilesize))
    maintiledir = None
    for tilex, tiley in product(tilexrange, tileyrange):
        if (tilex < myRegion.tiles['xmin']) or (tilex >= myRegion.tiles['xmax']) or \
           (tiley < myRegion.tiles['ymin']) or (tiley >= myRegion.tiles['ymax']):
            continue
        tiledir = os.path.join('Regions', name, 'Tiles', '%dx%d' % (tilex, tiley))
        if not(os.path.exists(tiledir)) or not(os.path.exists(os.path.join(tiledir, 'level.dat'))):
            log.log_debug(1,"Skipping missing tile" % (tilex, tiley))
            continue
        if maintiledir == None:
            maintiledir = tiledir
            maintileworld = mclevel.MCInfdevOldLevel(tiledir, create=False)
            # If this hack is necessary, it's dumb. It seems to be.
            maintileworld.copyBlocksFrom(maintileworld, maintileworld.bounds, \
                                         maintileworld.bounds.origin)
        else:
            log.log_debug(2,"Copying tile %dx%d to %s" % (tilex, tiley, maintiledir))
            tileworld = mclevel.MCInfdevOldLevel(tiledir, create=False)
            maintileworld.copyBlocksFrom(tileworld, tileworld.bounds, tileworld.bounds.origin)
            tileworld = None

    if maintiledir == None:
        log.log_debug(1,"Region (%d,%d) appears to be complete; skipping." % (regionx, regiony))
        return
    else:
        log.log_debug(2,"maintiledir is %s" % maintiledir)

    save(maintileworld)
    maintileworld = None
    srcdir = os.path.join(maintiledir, 'region')
    dstdir = os.path.join('Worlds', name, 'region')
    for path, dirlist, filelist in os.walk(srcdir):
        for name in fnmatch.filter(filelist, "*.mca"):
            shutil.move(os.path.join(path,name),dstdir)
            log.log_debug(2,"Moved file %s to %s" % (os.path.join(path,name),dstdir))
    os.remove(os.path.join(maintiledir,'level.dat'))
Beispiel #13
0
 def handle_events(self, events):
     for event in events:
         if event.type == QUIT:
             exit()
         elif event.type == KEYDOWN:
             if event.key == K_ESCAPE:
                 self.manager.go_to('levelSelectScreen')
             if self.player.finished:
                 if event.key == K_DOWN:
                     self.answerChoice = (self.answerChoice + 1) % 3
                 elif event.key == K_UP:
                     self.answerChoice = (self.answerChoice - 1) % 3
                 elif event.key == K_RETURN:
                     save([('powers',[self.player.timePower,self.player.mines,self.player.shields])])
                     self.manager.go_to_game(self.level.index + 1 )
Beispiel #14
0
    def store(self, response):

        headers = dict(response.headers.copy())
        headers['url'] = response.url
        if response.encoding:
            headers['encoding'] = response.encoding.lower()
        headers['status-code'] = int(response.status_code)

        filename = self._fn(response.url)
        utils.save('%s.metadata' % filename, 
                        content=json.dumps(headers, indent=4, sort_keys=True) + '\n')
        utils.save(filename, 
                        headers=response.headers, 
                        content=response.content)
        return True
 def display(self):  # called each loop
     if g.big:
         g.screen.fill(self.colors[1])
         utils.centre_blit(g.screen, g.big_surface, (g.sx(16), g.sy(11.5)))
     else:
         if self.sugar:
             g.screen.fill(self.colors[1])
         else:
             g.screen.blit(g.bgd, (g.sx(0), 0))
         g.screen.blit(g.box, (g.x0, g.y0))
         g.screen.blit(g.box, (g.x1, g.y1))
         if not self.sugar:
             utils.centre_blit(g.screen, g.magician, g.magician_c)
         self.draw_goal()
         utils.centre_blit(g.screen, g.turtle,
                           (g.x0 + 4 * g.dd, g.y0 + 6 * g.dd))
         self.tu.draw()
         if self.tu.win:
             if self.sugar:
                 if self.good_job is None:
                     path = self.parent.good_job_image_path()
                     self.good_job = utils.load_image(path, True)
                 if g.w > g.h:
                     utils.centre_blit(
                         g.screen, self.good_job, (g.sx(7), g.sy(17)))
                 else:
                     utils.centre_blit(g.screen, self.good_job, (g.sx(7),
                                                                 g.sy(38)))
             else:
                 utils.centre_blit(g.screen, g.smiley, (g.sx(16.6),
                                                        g.sy(2.2)))
             if self.sugar:
                 self.cyan_button.set_sensitive(True)
             else:
                 buttons.on('cyan')
             if not self.journal:
                 utils.save()
         self.draw_nos()
         if not self.sugar:
             buttons.draw()
             self.slider.draw()
         if g.score > 0:
             if self.sugar:
                 self.parent.update_score(int(g.score))
             else:
                 utils.display_score()
         utils.display_number1(g.pattern, (g.sx(2.4), g.sy(2)),
                               g.font1, utils.WHITE)
Beispiel #16
0
def main(fn, output_fn):
    print("Reading in dataset")
    data, classes = readDataset(fn)
    print(len(data), " sequences found")
    print("Found classes:", sorted(classes))
    proc = Processor(classes, 2, 2, prefix=(1,3), affix=(2,1), hashes=2,
            features=100000, stem=False, ohe=False)

    yss = []
    ryss = []
    for Xs in data:
        ys = [x['output'] for x in Xs]
        yss.append(ys)
        ryss.append([proc.encode_target(ys, i) for i in range(len(ys))])

    rs = np.random.RandomState(seed=2016)
    print("Starting KFolding")
    y_trues, y_preds = [], []
    fold_object = KFold(5, random_state=1)
    for train_idx, test_idx in fold_object.split(data):
        tr_X, tr_y = subset(data, yss, train_idx, rs)
        test_data = subset(data, yss, test_idx, rs, False)

        print("Training")
        d = Dagger(proc, tr_X, tr_y, validation_set=test_data)
        clf = d.train(10)

        seq = Sequencer(proc, clf)

        print("Testing")
        y_true, y_pred = test(data, ryss, test_idx, seq)
#        print(y_true, y_pred, proc.labels)
        print( classification_report(y_true, y_pred))

        y_trues.extend(y_true)
        y_preds.extend(y_pred)

    print("Total Report")
    print(classification_report(y_trues, y_preds, target_names=proc.labels))

    print("Training all")
    idxs = range(len(data))
    tr_X, tr_y = subset(data, yss, idxs, rs)
    d = Dagger(proc, tr_X, tr_y)
    clf = d.train()
    seq = Sequencer(proc, clf)

    save(output_fn, seq)
Beispiel #17
0
def investigate_laplacian(graph):
    """ Compute Laplacian
    """
    w = nx.laplacian_spectrum(graph)

    pairs = []
    for i, w in enumerate(sorted(w)):
        if abs(w) < 1e-5: continue
        inv_w = 1 / w
        pairs.append((inv_w, i))

    fig = plt.figure()
    plt.scatter(*zip(*pairs))
    plt.xlabel(r'$\frac{1}{\lambda_i}$')
    plt.ylabel(r'rank index')
    save(fig, 'laplacian_spectrum')
    def test(self, codebook, svm, des_option = constants.ORB_FEAT_OPTION, is_interactive=True):
        """
        Gets the descriptors for the testing set and use the svm given as a parameter to predict all the elements

        Args:
            codebook (NumPy matrix): Each row is a center of a codebook of Bag of Words approach.
            svm (cv2.SVM): The Support Vector Machine obtained in the training phase.
            des_option (integer): The option of the feature that is going to be used as local descriptor.
            is_interactive (boolean): If it is the user can choose to load files or generate.

        Returns:
            NumPy float array: The result of the predictions made.
            NumPy float array: The real labels for the testing set.
        """
        des_name = constants.ORB_FEAT_NAME if des_option == constants.ORB_FEAT_OPTION else constants.SIFT_FEAT_NAME
        k = len(codebook)
        x_filename = filenames.vlads_test(k, des_name)
        if is_interactive:
            data_option = input("Enter [1] to calculate VLAD vectors for the testing set or [2] to load them.\n")
        else:
            data_option = constants.GENERATE_OPTION
        if data_option == constants.GENERATE_OPTION:
            # Getting the global vectors for all of the testing set
            print("Getting global descriptors for the testing set...")
            start = time.time()
            x, y = self.get_data_and_labels(self.dataset.get_test_set(), codebook, des_option)
            utils.save(x_filename, x)
            end = time.time()
            print("VLADs testing vectors saved on file {0}".format(x_filename))
            self.log.test_vlad_time(end - start)
        else:
            # Loading the global vectors for all of the testing set
            print("Loading global descriptors for the testing set.")
            x = utils.load(x_filename.format(des_name))
            y = self.dataset.get_test_y()
            x = np.matrix(x, dtype=np.float32)
        # Predicting the testing set using the SVM
        start = time.time()
        result = svm.predict_all(x)
        end = time.time()
        self.log.predict_time(end - start)
        mask = result == y
        correct = np.count_nonzero(mask)
        accuracy = (correct * 100.0 / result.size)
        self.log.accuracy(accuracy)
        return result, y
Beispiel #19
0
 def handle_events(self, events):
     for event in events:
         if event.type == KEYDOWN:
             if event.key == K_ESCAPE:
                 self.manager.go_to('startScreen')
             elif event.key == K_RETURN:
                 if self.menuChoice == 0:
                     # self.manager.go_to('levelSelectScreen')
                     save([('powers',[0,0,0])])
                     self.manager.go_to_game(0)
                 elif self.menuChoice == 1:
                     self.manager.go_to('optionsScreen')
                 elif self.menuChoice == 2:
                     exit()
             elif event.key == K_UP:
                 self.menuChoice = (self.menuChoice - 1) % len(self.menuEntries)
             elif event.key == K_DOWN:
                 self.menuChoice = (self.menuChoice + 1) % len(self.menuEntries)
Beispiel #20
0
	def fitModel(self,trX,trY,snapshot_rate=1,path=None,epochs=30,batch_size=50,learning_rate_decay=0.97,decay_after=10):
		X_minibatch=[]
		Y_minibatch=[]
		num_examples = trX.shape[1]
		batches_in_one_epoch = int(num_examples / batch_size)
		
		loss_values = []
		for epoch in range(epochs):
			t0 = time.time()
			permutation = permute(num_examples)
			if self.X.ndim > 2:
				trX = trX[:,permutation,:]
			else:
				trX = trX[:,permutation]
			
			if self.Y.ndim > 1:
				trY = trY[:,permutation]
			else:
				trY = trY[permutation]
			
			for j in range(batches_in_one_epoch):
				if self.X.ndim > 2:
					X_minibatch = trX[:,j*batch_size:(j+1)*batch_size,:]
				else:
					X_minibatch = trX[:,j*batch_size:(j+1)*batch_size]
				if self.Y.ndim > 1:
					Y_minibatch = trY[:,j*batch_size:(j+1)*batch_size]
				else:
					Y_minibatch = trY[j*batch_size:(j+1)*batch_size]

				loss = self.train(X_minibatch,Y_minibatch)
				loss_values.append(loss)
				print "epoch={0} loss={1}".format(epoch,loss)
				
			if path and epoch % snapshot_rate == 0:
				print 'saving snapshot checkpoint.{0}'.format(epoch)
				save(self,"{0}checkpoint.{1}".format(path,epoch))
				f = open('{0}logfile'.format(path),'w')
				for v in loss_values:
					f.write('{0}\n'.format(v))
				f.close()
			t1 = time.time()
			print 'Epoch took {0} seconds'.format(t1-t0)
Beispiel #21
0
def downloadLectures(url):
    lectures = getLectures(url)
    print("Processing Lectures")
    os.mkdir("lectures")
    for k, v in lectures.items():
        dirpath = mkdir("lectures", k)
        for item in v:
            print("Processing %s/%s" % (k, item["Heiti"]))
            path = mkdir(dirpath, item["Heiti"])
            for link, title in {"Glærur": "slides","3/síðu": "3_page","6/síðu": "6_page","Hljóðgl.": "video","Annað": "other"}.items():
                l = item[link]
                if not l or len(l) != 2: continue

                _, url = l
                if "?Page=Download" in url:
                    save(path, url)
                elif "Action=16" in url:
                    saveRedirect(os.path.join(path, cleanFile(title) + ".html"), url)
                else:
                    saveLink(os.path.join(path, cleanFile(title) + ".html"), url)
Beispiel #22
0
def prepare_target_users():
    target_data = "data/target_users.pkl"
    seed_data = "data/seed_users.pkl"

    if os.path.exists(target_data):
        targets_info = cPickle.load(open(target_data))
    else:
        if not os.path.exists(seed_data):
            tracks = cPickle.load(open("data/recent_tracks.pkl", "rb"))
            seeds = get_seed_users(tracks)
            print "total %d seed users" % len(seeds)
            save(seed_data, seeds)
        else:
            seeds = cPickle.load(open(seed_data))
        targets_info = get_target_users(seeds)
        targets_info = {k: v for k, v in targets_info.iteritems()
                        if filter_target_info(v)}
        print "total %d target users" % len(target_data)
        save(target_data, targets_info)
    return targets_info
Beispiel #23
0
def downloadAssignments(url, handin=False):
    assignments = getAssignments(url)
    os.mkdir("assignments")
    for k, v in assignments.items():
        dirpath = mkdir("assignments", k)
        for item in v:
            print("Processing %s/%s" % (k, item["Nafn"][0]))
            path = mkdir(dirpath, item["Nafn"][0])
            assignment = getAssignment(item["Nafn"][1])

            # Description
            descr = os.path.join(path, "description")
            os.mkdir(descr)
            jsondump(os.path.join(descr,"description.json"), assignment["description"])
            genHtml(os.path.join(descr, "description.html"), assignment["description"]["description"], assignment["description"]["title"])
            for item in assignment["description"]["files"]:
                save(descr, item["url"])

            if handin:
                if "grade" in assignment:
                    grade = os.path.join(path, "grade")
                    os.mkdir(grade)
                    jsondump(os.path.join(grade, "grade.json"), assignment["grade"])
                    for item in assignment["grade"]["files"]:
                        save(grade, item["url"])

                if "handin" in assignment:
                    handin = os.path.join(path, "handin")
                    os.mkdir(handin)
                    jsondump(os.path.join(handin, "handin.json"), assignment["handin"])

                if "statistics" in assignment:
                    jsondump(os.path.join(path, "stats.json"), assignment["statistics"])
                    if assignment["statistics"]["image"] is not None:
                        save(os.path.join(path, "stats.jpg"), assignment["statistics"]["image"])
Beispiel #24
0
def calc_herm_modforms(**kwargs):
	kwargs = kwargs.copy()
	from sage.parallel.ncpus import ncpus
	for key,value in {
		"D": -3,
		"HermWeight": 6,
		"B_cF": 7,
		"task_limit": ncpus(),
	}.items():
		if not key in kwargs:
			kwargs[key] = value
	print "Calculating Hermitian modular forms with %r" % kwargs
	import algo
	modforms = algo.herm_modform_space__parallel(**kwargs)
	assert modforms
	import utils
	utils.save(
		modforms,
		"herm_modforms__D%i_k%i_B%i__%i.sobj" %
		(kwargs["D"], kwargs["HermWeight"], kwargs["B_cF"], modforms.degree())
	)
	print modforms
	sys.exit(0)
Beispiel #25
0
 def display(self):  # called each loop
     if g.big:
         g.screen.fill(self.colors[1])
         utils.centre_blit(g.screen, g.big_surface, (g.sx(16), g.sy(11.5)))
     else:
         if self.sugar:
             g.screen.fill(self.colors[1])
         else:
             g.screen.blit(g.bgd, (g.sx(0), 0))
         g.screen.blit(g.box, (g.x0, g.y0))
         g.screen.blit(g.box, (g.x1, g.y1))
         if not self.sugar:
             utils.centre_blit(g.screen, g.magician, g.magician_c)
         self.draw_goal()
         utils.centre_blit(g.screen, g.turtle,
                           (g.x0 + 4 * g.dd, g.y0 + 6 * g.dd))
         self.tu.draw()
         if self.tu.win:
             utils.centre_blit(g.screen, g.smiley, (g.sx(16.6), g.sy(2.2)))
             if self.sugar:
                 self.cyan_button.set_sensitive(True)
             else:
                 buttons.on('cyan')
             if not self.journal:
                 utils.save()
         self.draw_nos()
         if not self.sugar:
             buttons.draw()
             self.slider.draw()
         if g.score > 0:
             if self.sugar:
                 self.label.set_markup(
                     '<span><big><b> %s</b></big></span>' % (str(g.score)))
             else:
                 utils.display_score()
         utils.display_number1(g.pattern, (g.sx(2.4), g.sy(2)),
                               g.font1, utils.BLUE)
Beispiel #26
0
def investigate_skip_influence(max_skip=100, reps_per_config=50):
    """ Investigate how number of skipped entries influence reconstruction accuracy
    """
    # setup network
    graph = generate_ring_graph(3)

    dim = len(graph.nodes())
    Bvec = np.random.uniform(0, 5, size=dim)
    omega = np.random.uniform(0, 5, size=dim)

    system_config = get_base_config(nx.to_numpy_matrix(graph), Bvec, omega)

    syst = DW({
        'graph': graph,
        'system_config': system_config
    })

    # generate data
    df = pd.DataFrame(columns=['skip', 'parameter', 'relative_error'])
    for i in tqdm(range(1, max_skip)):
        for _ in trange(reps_per_config):
            res = process([syst], skipper=i)
            err_A, err_B = compute_error(res)

            df = df.append([
                {'skip': i, 'parameter': 'A', 'relative_error': err_A},
                {'skip': i, 'parameter': 'B', 'relative_error': err_B}
            ], ignore_index=True)

    # plot result
    fig = plt.figure(figsize=(12,6))

    sns.boxplot(x='skip', y='relative_error', hue='parameter', data=df)
    plt.xticks(rotation=90, fontsize=6)

    save(fig, 'reconstruction_skip')
Beispiel #27
0
def plot_result(data):
    """ Plot final result
    """
    # main overview
    fig = plt.figure(figsize=(30, 10))
    gs = mpl.gridspec.GridSpec(2, 3, width_ratios=[1, 1, 2])

    plot_graph(
        data.graph, plt.subplot(gs[:, 0]))
    plot_matrix(
        data.syncs, plt.subplot(gs[:, 1]),
        title='Sign-switch of dynamic connectivity matrix',
        allow_negative=False)
    plot_evolutions(
        data.sols[0], data.ts, plt.subplot(gs[0, 2]))
    plot_correlation_matrix(
        np.mean(data.cmats, axis=0), data.ts, plt.subplot(gs[1, 2]))

    plt.tight_layout()
    save(fig, 'overview_plot')

    # variance investigation
    fig = plt.figure(figsize=(20, 10))
    gs = mpl.gridspec.GridSpec(2, 1)

    plot_evolutions(data.sols[0], data.ts, plt.subplot(gs[0]))
    plot_series(data.vser, data.ts, plt.subplot(gs[1]))

    plt.tight_layout()
    save(fig, 'cluster_development')

    # correlation matrix heatmap
    fig = plt.figure()

    cmat_sum = np.sum(np.mean(data.cmats, axis=0), axis=2)
    plot_matrix(
        cmat_sum, plt.gca(),
        title='Summed correlation matrix')

    plt.tight_layout()
    save(fig, 'correlation_matrix')
Beispiel #28
0
def get_target_friends(targets_info):
    target_data = "data/target_users.pkl"

    target_friends = {}
    friends_info = {}
    invalid_targets = []

    count = 0
    for name in targets_info:
        result = get_user_friends(name)
        if result is not None:
            targets_info[name]['friend_nums'] = len(result[0])
            target_friends[name] = result[0].keys()
            friends_info.update(result[0])
            count += 1
            if count == 1000:
                break
        else:
            invalid_targets.append(name)

    for name in targets_info.keys():
        if name not in target_friends:
            del targets_info[name]

    for name in invalid_targets:
        del targets_info[name]

    # if len(targets_info) > 1000:
        # keys = random.sample(targets_info, 1000)
        # targets_info = {k: v for k, v in targets_info.iteritems()
        # if k in keys}

    save(target_data, targets_info)
    print "--- total %d targets ---" % len(targets_info)
    save("data/target_friends.pkl", target_friends)
    count = sum(len(fr) for fr in target_friends.itervalues())
    print "--- all targets have %d friend counts ---" % count
    save("data/friends_info.pkl", friends_info)
    print "--- total %d friends ---" % len(friends_info)
Beispiel #29
0
 def handle_events(self, events):
     for event in events:
         if event.type == KEYDOWN:
             if event.key == K_ESCAPE:
                 save([('soundLevel',self.soundLevel),('musicLevel',self.musicLevel)])
                 self.manager.go_to('mainMenuScreen')
             if event.key == K_RETURN:
                 if self.menuChoice == 3:
                     save([('soundLevel',self.soundLevel),('musicLevel',self.musicLevel)])
                     self.manager.go_to('mainMenuScreen')
             if event.key == K_UP:
                 self.menuChoice = (self.menuChoice - 1) % len(self.menuEntries)
             if event.key == K_DOWN:
                 self.menuChoice = (self.menuChoice + 1) % len(self.menuEntries)
             if event.key == K_LEFT:
                 if self.menuChoice == 0:
                     self.soundLevel = max(0,self.soundLevel-1)
                     self.volumeTest.set_volume(self.soundLevel*0.2)
                     self.volumeTest.play()
                 if self.menuChoice == 1:
                     self.musicLevel = max(0,self.musicLevel-1)
                     pygame.mixer.music.set_volume(self.musicLevel*0.2)
                     self.volumeTest.set_volume(self.musicLevel*0.2)
                     self.volumeTest.play()
             if event.key == K_RIGHT:
                 if self.menuChoice == 0:
                     self.soundLevel = min(5,self.soundLevel+1)
                     self.volumeTest.set_volume(self.soundLevel*0.2)
                     self.volumeTest.play()
                 if self.menuChoice == 1:
                     self.musicLevel = min(5,self.musicLevel+1)
                     pygame.mixer.music.set_volume(self.musicLevel*0.2)
                     self.volumeTest.set_volume(self.musicLevel*0.2)
                     self.volumeTest.play()
         if event.type == QUIT:
             save([('soundLevel',self.soundLevel),('musicLevel',self.musicLevel)])
Beispiel #30
0
 def f_save(step):
   save(sess, saver, checkpoint_dir, step, name)
   T.save_weights(checkpoint_dir+"/T_weights.keras", True)
 def save(self, path_model, path_pca, path_res=None):
     self.model.save(path_model)
     import utils
     utils.save(path_pca, self.pca)
     utils.save(path_res, self.res)
Beispiel #32
0
def main():
    def _str_to_bool(s):
        """Convert string to bool (in argparse context)."""
        if s.lower() not in ['true', 'false']:
            raise ValueError(
                'Argument needs to be a boolean, got {}'.format(s))
        return {'true': True, 'false': False}[s.lower()]

    parser = argparse.ArgumentParser(description='WaveNet example network')

    DATA_DIRECTORY = './data/kss,./data/son'
    parser.add_argument('--data_dir',
                        type=str,
                        default=DATA_DIRECTORY,
                        help='The directory containing the VCTK corpus.')

    LOGDIR = None
    #LOGDIR = './/logdir-wavenet//train//2018-12-21T22-58-10'

    parser.add_argument(
        '--logdir',
        type=str,
        default=LOGDIR,
        help=
        'Directory in which to store the logging information for TensorBoard. If the model already exists, it will restore the state and will continue training. Cannot use with --logdir_root and --restore_from.'
    )

    parser.add_argument(
        '--logdir_root',
        type=str,
        default=None,
        help=
        'Root directory to place the logging output and generated model. These are stored under the dated subdirectory of --logdir_root. Cannot use with --logdir.'
    )
    parser.add_argument(
        '--restore_from',
        type=str,
        default=None,
        help=
        'Directory in which to restore the model from. This creates the new model under the dated directory in --logdir_root. Cannot use with --logdir.'
    )

    CHECKPOINT_EVERY = 1000  # checkpoint 저장 주기
    parser.add_argument(
        '--checkpoint_every',
        type=int,
        default=CHECKPOINT_EVERY,
        help='How many steps to save each checkpoint after. Default: ' +
        str(CHECKPOINT_EVERY) + '.')

    config = parser.parse_args()  # command 창에서 입력받을 수 있는 조건
    config.data_dir = config.data_dir.split(",")

    try:
        directories = validate_directories(config, hparams)
    except ValueError as e:
        print("Some arguments are wrong:")
        print(str(e))
        return

    logdir = directories['logdir']
    restore_from = directories['restore_from']

    # Even if we restored the model, we will treat it as new training
    # if the trained model is written into an arbitrary location.
    is_overwritten_training = logdir != restore_from

    log_path = os.path.join(logdir, 'train.log')
    infolog.init(log_path, logdir)

    global_step = tf.Variable(0, name='global_step', trainable=False)

    # Create coordinator.
    coord = tf.train.Coordinator()
    num_speakers = len(config.data_dir)
    # Load raw waveform from VCTK corpus.
    with tf.name_scope('create_inputs'):
        # Allow silence trimming to be skipped by specifying a threshold near
        # zero.
        silence_threshold = hparams.silence_threshold if hparams.silence_threshold > EPSILON else None
        gc_enable = num_speakers > 1

        # AudioReader에서 wav 파일을 잘라 input값을 만든다. receptive_field길이만큼을 앞부분에 pad하거나 앞조각에서 가져온다. (receptive_field+ sample_size)크기로 자른다.
        reader = DataFeederWavenet(
            coord,
            config.data_dir,
            batch_size=hparams.wavenet_batch_size,
            receptive_field=WaveNetModel.calculate_receptive_field(
                hparams.filter_width, hparams.dilations, hparams.scalar_input,
                hparams.initial_filter_width),
            gc_enable=gc_enable)
        if gc_enable:
            audio_batch, lc_batch, gc_id_batch = reader.inputs_wav, reader.local_condition, reader.speaker_id
        else:
            print("didn't work")
            #audio_batch, lc_batch = reader.inputs_wav, local_condition

    # Create network.
    net = WaveNetModel(
        batch_size=hparams.wavenet_batch_size,
        dilations=hparams.dilations,
        filter_width=hparams.filter_width,
        residual_channels=hparams.residual_channels,
        dilation_channels=hparams.dilation_channels,
        quantization_channels=hparams.quantization_channels,
        out_channels=hparams.out_channels,
        skip_channels=hparams.skip_channels,
        use_biases=hparams.use_biases,  #  True
        scalar_input=hparams.scalar_input,
        initial_filter_width=hparams.initial_filter_width,
        global_condition_channels=hparams.gc_channels,
        global_condition_cardinality=num_speakers,
        local_condition_channels=hparams.num_mels,
        upsample_factor=hparams.upsample_factor,
        train_mode=True)

    if hparams.l2_regularization_strength == 0:
        hparams.l2_regularization_strength = None

    net.add_loss(input_batch=audio_batch,
                 local_condition=lc_batch,
                 global_condition_batch=gc_id_batch,
                 l2_regularization_strength=hparams.l2_regularization_strength)
    net.add_optimizer(hparams, global_step)

    run_metadata = tf.RunMetadata()

    # Set up session
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False)
                      )  # log_device_placement=False --> cpu/gpu 자동 배치.
    init = tf.global_variables_initializer()
    sess.run(init)

    # Saver for storing checkpoints of the model.
    saver = tf.train.Saver(
        var_list=tf.global_variables(),
        max_to_keep=hparams.max_checkpoints)  # 최대 checkpoint 저장 갯수 지정

    try:
        start_step = load(saver, sess, restore_from)  # checkpoint load
        if is_overwritten_training or start_step is None:
            # The first training step will be saved_global_step + 1,
            # therefore we put -1 here for new or overwritten trainings.
            zero_step_assign = tf.assign(global_step, 0)
            sess.run(zero_step_assign)

    except:
        print(
            "Something went wrong while restoring checkpoint. We will terminate training to avoid accidentally overwriting the previous model."
        )
        raise

    ###########

    start_step = sess.run(global_step)
    last_saved_step = start_step
    try:
        reader.start_in_session(sess, start_step)
        while not coord.should_stop():

            start_time = time.time()
            if hparams.store_metadata and step % 50 == 0:
                # Slow run that stores extra information for debugging.
                log('Storing metadata')
                run_options = tf.RunOptions(
                    trace_level=tf.RunOptions.FULL_TRACE)
                step, loss_value, _ = sess.run(
                    [global_step, net.loss, net.optimize],
                    options=run_options,
                    run_metadata=run_metadata)

                tl = timeline.Timeline(run_metadata.step_stats)
                timeline_path = os.path.join(logdir, 'timeline.trace')
                with open(timeline_path, 'w') as f:
                    f.write(tl.generate_chrome_trace_format(show_memory=True))
            else:
                step, loss_value, _ = sess.run(
                    [global_step, net.loss, net.optimize])

            duration = time.time() - start_time
            log('step {:d} - loss = {:.3f}, ({:.3f} sec/step)'.format(
                step, loss_value, duration))

            if step % config.checkpoint_every == 0:
                save(saver, sess, logdir, step)
                last_saved_step = step

            if step >= hparams.num_steps:
                # error message가 나오지만, 여기서 멈춘 것은 맞다.
                raise Exception('End xxx~~~yyy')

    except Exception as e:
        print('finally')
        #if step > last_saved_step:
        #    save(saver, sess, logdir, step)

        coord.request_stop(e)
Beispiel #33
0
def main():
    # gpu device check
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)

    # init
    np.random.seed(args.seed)  # set random seed
    torch.cuda.set_device(args.gpu)  # set training device
    cudnn.benchmark = True  # set cudnn benchmark
    torch.manual_seed(args.seed)  # set torch random seed
    cudnn.enabled = True  # set cudnn
    torch.cuda.manual_seed(args.seed)  # set torch cuda seed
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)

    # search space
    genotype = eval("genotypes.%s" % args.arch)  # TODO convert to tree
    model = Network(args.init_channels, CIFAR_CLASSES, args.layers,
                    args.auxiliary, genotype)
    model = model.cuda()

    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    optimizer = torch.optim.SGD(model.parameters(),
                                args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    train_transform, valid_transform = utils._data_transforms_cifar10(args)
    train_data = dset.CIFAR10(root=args.data,
                              train=True,
                              download=True,
                              transform=train_transform)
    valid_data = dset.CIFAR10(root=args.data,
                              train=False,
                              download=True,
                              transform=valid_transform)

    train_queue = torch.utils.data.DataLoader(train_data,
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              pin_memory=True,
                                              num_workers=2)

    valid_queue = torch.utils.data.DataLoader(valid_data,
                                              batch_size=args.batch_size,
                                              shuffle=False,
                                              pin_memory=True,
                                              num_workers=2)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs))

    for epoch in range(args.epochs):
        scheduler.step()
        logging.info('epoch %d lr %e', epoch, scheduler.get_lr()[0])
        model.drop_path_prob = args.drop_path_prob * epoch / args.epochs

        train_acc, train_obj = train(train_queue, model, criterion, optimizer)
        logging.info('train_acc %f', train_acc)

        valid_acc, valid_obj = infer(valid_queue, model, criterion)
        logging.info('valid_acc %f', valid_acc)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
Beispiel #34
0
def main(args):

    # 0. initial setting

    # set environmet
    cudnn.benchmark = True

    if not os.path.isdir('./ckpt'):
        os.mkdir('./ckpt')
    if not os.path.isdir('./results'):
        os.mkdir('./results')
    if not os.path.isdir(os.path.join('./ckpt', args.name)):
        os.mkdir(os.path.join('./ckpt', args.name))
    if not os.path.isdir(os.path.join('./results', args.name)):
        os.mkdir(os.path.join('./results', args.name))
    if not os.path.isdir(os.path.join('./results', args.name, "log")):
        os.mkdir(os.path.join('./results', args.name, "log"))

    # set logger
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(message)s')
    handler = logging.FileHandler("results/{}/log/{}.log".format(
        args.name, time.strftime('%c', time.localtime(time.time()))))
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.addHandler(logging.StreamHandler())
    args.logger = logger

    # set cuda
    if torch.cuda.is_available():
        args.logger.info("running on cuda")
        args.device = torch.device("cuda")
        args.use_cuda = True
    else:
        args.logger.info("running on cpu")
        args.device = torch.device("cpu")
        args.use_cuda = False

    args.logger.info("[{}] starts".format(args.name))

    # 1. load data

    args.logger.info("loading data...")
    src, tgt = load_data(args.path)

    src_vocab = Vocab(init_token='<sos>',
                      eos_token='<eos>',
                      pad_token='<pad>',
                      unk_token='<unk>')
    src_vocab.load(os.path.join(args.path, 'vocab.en'))
    tgt_vocab = Vocab(init_token='<sos>',
                      eos_token='<eos>',
                      pad_token='<pad>',
                      unk_token='<unk>')
    tgt_vocab.load(os.path.join(args.path, 'vocab.de'))

    # 2. setup

    args.logger.info("setting up...")

    sos_idx = 0
    eos_idx = 1
    pad_idx = 2
    max_length = 50

    src_vocab_size = len(src_vocab)
    tgt_vocab_size = len(tgt_vocab)

    # transformer config
    d_e = 512  # embedding size
    d_q = 64  # query size (= key, value size)
    d_h = 2048  # hidden layer size in feed forward network
    num_heads = 8
    num_layers = 6  # number of encoder/decoder layers in encoder/decoder

    args.sos_idx = sos_idx
    args.eos_idx = eos_idx
    args.pad_idx = pad_idx
    args.max_length = max_length
    args.src_vocab_size = src_vocab_size
    args.tgt_vocab_size = tgt_vocab_size
    args.d_e = d_e
    args.d_q = d_q
    args.d_h = d_h
    args.num_heads = num_heads
    args.num_layers = num_layers

    model = Transformer(args)
    model.to(args.device)
    loss_fn = nn.CrossEntropyLoss(ignore_index=pad_idx)
    optimizer = optim.Adam(model.parameters(), lr=1e-5)

    if args.load:
        model.load_state_dict(load(args, args.ckpt))

    # 3. train / test

    if not args.test:
        # train
        args.logger.info("starting training")
        acc_val_meter = AverageMeter(name="Acc-Val (%)",
                                     save_all=True,
                                     save_dir=os.path.join(
                                         'results', args.name))
        train_loss_meter = AverageMeter(name="Loss",
                                        save_all=True,
                                        save_dir=os.path.join(
                                            'results', args.name))
        train_loader = get_loader(src['train'],
                                  tgt['train'],
                                  src_vocab,
                                  tgt_vocab,
                                  batch_size=args.batch_size,
                                  shuffle=True)
        valid_loader = get_loader(src['valid'],
                                  tgt['valid'],
                                  src_vocab,
                                  tgt_vocab,
                                  batch_size=args.batch_size)

        for epoch in range(1, 1 + args.epochs):
            spent_time = time.time()
            model.train()
            train_loss_tmp_meter = AverageMeter()
            for src_batch, tgt_batch in tqdm(train_loader):
                # src_batch: (batch x source_length), tgt_batch: (batch x target_length)
                optimizer.zero_grad()
                src_batch, tgt_batch = torch.LongTensor(src_batch).to(
                    args.device), torch.LongTensor(tgt_batch).to(args.device)
                batch = src_batch.shape[0]
                # split target batch into input and output
                tgt_batch_i = tgt_batch[:, :-1]
                tgt_batch_o = tgt_batch[:, 1:]

                pred = model(src_batch.to(args.device),
                             tgt_batch_i.to(args.device))
                loss = loss_fn(pred.contiguous().view(-1, tgt_vocab_size),
                               tgt_batch_o.contiguous().view(-1))
                loss.backward()
                optimizer.step()

                train_loss_tmp_meter.update(loss / batch, weight=batch)

            train_loss_meter.update(train_loss_tmp_meter.avg)
            spent_time = time.time() - spent_time
            args.logger.info(
                "[{}] train loss: {:.3f} took {:.1f} seconds".format(
                    epoch, train_loss_tmp_meter.avg, spent_time))

            # validation
            model.eval()
            acc_val_tmp_meter = AverageMeter()
            spent_time = time.time()

            for src_batch, tgt_batch in tqdm(valid_loader):
                src_batch, tgt_batch = torch.LongTensor(
                    src_batch), torch.LongTensor(tgt_batch)
                tgt_batch_i = tgt_batch[:, :-1]
                tgt_batch_o = tgt_batch[:, 1:]

                with torch.no_grad():
                    pred = model(src_batch.to(args.device),
                                 tgt_batch_i.to(args.device))

                corrects, total = val_check(
                    pred.max(dim=-1)[1].cpu(), tgt_batch_o)
                acc_val_tmp_meter.update(100 * corrects / total, total)

            spent_time = time.time() - spent_time
            args.logger.info(
                "[{}] validation accuracy: {:.1f} %, took {} seconds".format(
                    epoch, acc_val_tmp_meter.avg, spent_time))
            acc_val_meter.update(acc_val_tmp_meter.avg)

            if epoch % args.save_period == 0:
                save(args, "epoch_{}".format(epoch), model.state_dict())
                acc_val_meter.save()
                train_loss_meter.save()
    else:
        # test
        args.logger.info("starting test")
        test_loader = get_loader(src['test'],
                                 tgt['test'],
                                 src_vocab,
                                 tgt_vocab,
                                 batch_size=args.batch_size)
        pred_list = []
        model.eval()

        for src_batch, tgt_batch in test_loader:
            #src_batch: (batch x source_length)
            src_batch = torch.Tensor(src_batch).long().to(args.device)
            batch = src_batch.shape[0]
            pred_batch = torch.zeros(batch, 1).long().to(args.device)
            pred_mask = torch.zeros(batch, 1).bool().to(
                args.device)  # mask whether each sentece ended up

            with torch.no_grad():
                for _ in range(args.max_length):
                    pred = model(
                        src_batch,
                        pred_batch)  # (batch x length x tgt_vocab_size)
                    pred[:, :, pad_idx] = -1  # ignore <pad>
                    pred = pred.max(dim=-1)[1][:, -1].unsqueeze(
                        -1)  # next word prediction: (batch x 1)
                    pred = pred.masked_fill(
                        pred_mask,
                        2).long()  # fill out <pad> for ended sentences
                    pred_mask = torch.gt(pred.eq(1) + pred.eq(2), 0)
                    pred_batch = torch.cat([pred_batch, pred], dim=1)
                    if torch.prod(pred_mask) == 1:
                        break

            pred_batch = torch.cat([
                pred_batch,
                torch.ones(batch, 1).long().to(args.device) + pred_mask.long()
            ],
                                   dim=1)  # close all sentences
            pred_list += seq2sen(pred_batch.cpu().numpy().tolist(), tgt_vocab)

        with open('results/pred.txt', 'w', encoding='utf-8') as f:
            for line in pred_list:
                f.write('{}\n'.format(line))

        os.system(
            'bash scripts/bleu.sh results/pred.txt multi30k/test.de.atok')
Beispiel #35
0
def main(args):
    # loading configurations
    with open(args.config) as f:
        config = yaml.safe_load(f)["configuration"]

    name = config["Name"]

    # Construct or load embeddings
    print("Initializing embeddings ...")
    vocab_size = config["embeddings"]["vocab_size"]
    embed_size = config["embeddings"]["embed_size"]
    embeddings = init_embeddings(vocab_size, embed_size, name=name)
    print("\tDone.")

    # Build the model and compute losses
    source_ids = tf.placeholder(tf.int32, [None, None], name="source")
    target_ids = tf.placeholder(tf.int32, [None, None], name="target")
    sequence_mask = tf.placeholder(tf.bool, [None, None], name="mask")
    choice_qs = tf.placeholder(tf.float32, [None, None], name="choice")
    emo_cat = tf.placeholder(tf.int32, [None], name="emotion_category")

    (enc_num_layers, enc_num_units, enc_cell_type, enc_bidir, dec_num_layers,
     dec_num_units, dec_cell_type, state_pass, num_emo, emo_cat_units,
     emo_int_units, infer_batch_size, beam_size, max_iter, attn_num_units,
     l2_regularize) = get_ECM_config(config)

    print("Building model architecture ...")
    CE, loss, train_outs, infer_outputs = compute_ECM_loss(
        source_ids, target_ids, sequence_mask, choice_qs, embeddings,
        enc_num_layers, enc_num_units, enc_cell_type, enc_bidir,
        dec_num_layers, dec_num_units, dec_cell_type, state_pass, num_emo,
        emo_cat, emo_cat_units, emo_int_units, infer_batch_size, beam_size,
        max_iter, attn_num_units, l2_regularize, name)
    print("\tDone.")

    # Even if we restored the model, we will treat it as new training
    # if the trained model is written into an arbitrary location.
    (logdir, restore_from, learning_rate, gpu_fraction, max_checkpoints,
     train_steps, batch_size, print_every, checkpoint_every, s_filename,
     t_filename, q_filename, c_filename, s_max_leng, t_max_leng,
     dev_s_filename, dev_t_filename, dev_q_filename, dev_c_filename, loss_fig,
     perp_fig) = get_ECM_training_config(config)

    is_overwritten_training = logdir != restore_from

    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate,
                                       epsilon=1e-4)
    trainable = tf.trainable_variables()
    optim = optimizer.minimize(loss, var_list=trainable)

    # Set up session
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False,
                                            gpu_options=gpu_options))
    init = tf.global_variables_initializer()
    sess.run(init)

    # Saver for storing checkpoints of the model.
    saver = tf.train.Saver(var_list=tf.trainable_variables(),
                           max_to_keep=max_checkpoints)

    try:
        saved_global_step = load(saver, sess, restore_from)
        if is_overwritten_training or saved_global_step is None:
            # The first training step will be saved_global_step + 1,
            # therefore we put -1 here for new or overwritten trainings.
            saved_global_step = -1

    except Exception:
        print("Something went wrong while restoring checkpoint. "
              "Training is terminated to avoid the overwriting.")
        raise

    # ##### Training #####
    # Load data
    print("Loading data ...")

    # id_0, id_1, id_2 preserved for SOS, EOS, constant zero padding
    embed_shift = 3

    source_data = loadfile(s_filename, is_source=True,
                           max_length=s_max_leng) + embed_shift
    target_data = loadfile(t_filename, is_source=False,
                           max_length=t_max_leng) + embed_shift

    choice_data = loadfile(q_filename, is_source=False, max_length=t_max_leng)
    choice_data[choice_data < 0] = 0
    choice_data = choice_data.astype(np.float32)

    category_data = pd.read_csv(c_filename,
                                header=None,
                                index_col=None,
                                dtype=int)[0].values

    masks = (target_data != -1)
    n_data = len(source_data)

    dev_source_data = None
    if dev_s_filename is not None:
        dev_source_data = loadfile(dev_s_filename,
                                   is_source=True,
                                   max_length=s_max_leng) + embed_shift
        dev_target_data = loadfile(dev_t_filename,
                                   is_source=False,
                                   max_length=t_max_leng) + embed_shift

        dev_choice_data = loadfile(dev_q_filename,
                                   is_source=False,
                                   max_length=t_max_leng)
        dev_choice_data[dev_choice_data < 0] = 0
        dev_choice_data = dev_choice_data.astype(np.float32)

        dev_category_data = pd.read_csv(dev_c_filename,
                                        header=None,
                                        index_col=None,
                                        dtype=int)[0].values

        dev_masks = (dev_target_data != -1)
    print("\tDone.")

    # Training
    last_saved_step = saved_global_step
    num_steps = saved_global_step + train_steps
    losses = []
    steps = []
    perps = []
    dev_perps = []

    print("Start training ...")
    try:
        for step in range(saved_global_step + 1, num_steps):
            start_time = time.time()
            rand_indexes = np.random.choice(n_data, batch_size)
            source_batch = source_data[rand_indexes]
            target_batch = target_data[rand_indexes]
            choice_batch = choice_data[rand_indexes]
            emotions = category_data[rand_indexes]
            mask_batch = masks[rand_indexes]

            feed_dict = {
                source_ids: source_batch,
                target_ids: target_batch,
                choice_qs: choice_batch,
                emo_cat: emotions,
                sequence_mask: mask_batch,
            }

            loss_value, _ = sess.run([loss, optim], feed_dict=feed_dict)
            losses.append(loss_value)

            duration = time.time() - start_time

            if step % print_every == 0:
                # train perplexity
                t_perp = compute_perplexity(sess, CE, mask_batch, feed_dict)
                perps.append(t_perp)

                # dev perplexity
                dev_str = ""
                if dev_source_data is not None:
                    dev_inds = np.random.choice(len(dev_source_data),
                                                batch_size)

                    dev_feed_dict = {
                        source_ids: dev_source_data[dev_inds],
                        target_ids: dev_target_data[dev_inds],
                        choice_qs: dev_choice_data[dev_inds],
                        emo_cat: dev_category_data[dev_inds],
                        sequence_mask: dev_masks[dev_inds],
                    }

                    dev_perp = compute_perplexity(sess, CE,
                                                  dev_masks[dev_inds],
                                                  dev_feed_dict)
                    dev_perps.append(dev_perp)
                    dev_str = "dev_prep: {:.3f}, ".format(dev_perp)

                steps.append(step)
                info = 'step {:d}, loss = {:.6f}, '
                info += 'perp: {:.3f}, {}({:.3f} sec/step)'
                print(info.format(step, loss_value, t_perp, dev_str, duration))

            if step % checkpoint_every == 0:
                save(saver, sess, logdir, step)
                last_saved_step = step

    except KeyboardInterrupt:
        # Introduce a line break after ^C so save message is on its own line.
        print()

    finally:
        if step > last_saved_step:
            save(saver, sess, logdir, step)

        # plot loss
        plt.figure()
        plt.plot(losses)
        plt.title("Total loss")
        plt.xlabel("step")
        plt.savefig(loss_fig)

        # plot perplexity
        plt.figure()
        if len(perps) > len(steps):
            perps.pop()
        plt.plot(steps[5:], perps[5:], label="train")
        if dev_source_data is not None:
            plt.plot(steps[5:], dev_perps[5:], label="dev")
        plt.title("Perplexity")
        plt.xlabel("step")
        plt.legend()
        plt.savefig(perp_fig)
Beispiel #36
0
    if show_max_img:
        print('******************分析宽度******************')
        show_dict(w_dict, 10, 'w')
        print('******************分析高度******************')
        show_dict(h_dict, 1, 'h')
        print('******************分析label长度******************')
        show_dict(len_dict, 1, 'label')
    a = ''.join(sorted(set((''.join(label_list)))))
    return a


if __name__ == '__main__':
    # 根据label文本生产key
    from config import train_parameters
    from utils import save

    parser = argparse.ArgumentParser()
    parser.add_argument('--label_file',
                        nargs='+',
                        help='label file',
                        default=[""])
    args = parser.parse_args()

    label_file = [
        train_parameters['train_list'], train_parameters['eval_list']
    ]
    alphabet = get_key(label_file, True, show_max_img=False).replace(' ', '')
    save(list(alphabet), 'dict.txt')
    # np.save('alphabet.npy', alphabet)
    print(alphabet)
Beispiel #37
0
def main():
    # if not torch.cuda.is_available():
    #     logging.info('no gpu device available')
    #     sys.exit(1)

    np.random.seed(args.seed)
    # torch.cuda.set_device(args.gpu)
    # cudnn.benchmark = True
    # torch.manual_seed(args.seed)
    # cudnn.enabled = True
    # torch.cuda.manual_seed(args.seed)
    # logging.info('gpu device = %d' % args.gpu)
    # logging.info("args = %s", args)

    torch.manual_seed(args.seed)
    logging.info('use cpu')
    logging.info("args = %s", args)

    criterion = nn.CrossEntropyLoss()
    # criterion = criterion.cuda()
    criterion.to(device)
    model = Network(args.init_channels,
                    CIFAR_CLASSES,
                    args.layers,
                    criterion,
                    learnable_bn=args.learnable_bn)
    # model = model.cuda()
    model.to(device)
    a = list(model.parameters())

    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    # optimizer = torch.optim.SGD(
    #     model.parameters(),
    #     args.learning_rate,
    #     momentum=args.momentum,
    #     weight_decay=args.weight_decay)

    ################################################################################
    # AdaS: optimizer and scheduler
    optimizer = SGDVec(params=model.parameters(),
                       lr=args.learning_rate,
                       momentum=args.momentum,
                       weight_decay=args.weight_decay)

    scheduler = AdaS(
        parameters=list(model.parameters()),
        init_lr=args.learning_rate,
        # min_lr=kwargs['min_lr'],
        # zeta=kwargs['zeta'],
        p=args.scheduler_p,
        beta=args.scheduler_beta)
    ################################################################################

    # train_transform, valid_transform = utils._data_transforms_cifar100(args)
    # train_data = dset.CIFAR10(root=args.data, train=True, download=True, transform=train_transform)
    """From https://github.com/chenxin061/pdarts/"""
    if args.cifar100:
        train_transform, valid_transform = utils._data_transforms_cifar100(
            args)
    else:
        train_transform, valid_transform = utils._data_transforms_cifar10(args)
    if args.cifar100:
        train_data = dset.CIFAR100(root=args.data,
                                   train=True,
                                   download=True,
                                   transform=train_transform)
    else:
        train_data = dset.CIFAR10(root=args.data,
                                  train=True,
                                  download=True,
                                  transform=train_transform)

    num_train = len(train_data)
    indices = list(range(num_train))
    split = int(np.floor(args.train_portion * num_train))

    train_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
        pin_memory=True,
        num_workers=2)

    valid_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(
            indices[split:num_train]),
        pin_memory=True,
        num_workers=2)

    # scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
    #     optimizer, float(args.epochs), eta_min=args.learning_rate_min)

    architect = Architect(model, args)
    """Hessian"""
    analyser = Analyzer(model, args)
    """adaptive stopping"""
    stop_checker = StopChecker()

    METRICS = Metrics(list(model.parameters()), p=1)

    PERFORMANCE_STATISTICS = {}
    ARCH_STATISTICS = {}
    GENOTYPE_STATISTICS = {}
    metrics_path = './metrics_stat_test_adas.xlsx'
    weights_path = './weights_stat_test_adas.xlsx'
    genotypes_path = './genotypes_stat_test_adas.xlsx'

    for epoch in range(args.epochs):
        # scheduler.step()
        # lr = scheduler.get_lr()[0]
        # logging.info

        genotype = model.genotype()
        logging.info('genotype = %s', genotype)
        if epoch % 5 == 0 or epoch == args.epochs - 1:
            GENOTYPE_STATISTICS[f'epoch_{epoch}'] = [genotype]
            genotypes_df = pd.DataFrame(data=GENOTYPE_STATISTICS)
            genotypes_df.to_excel(genotypes_path)

        print(F.softmax(model.alphas_normal, dim=-1))
        print(F.softmax(model.alphas_reduce, dim=-1))

        # training
        train_acc, train_obj = train(epoch, train_queue, valid_queue, model,
                                     architect, criterion, optimizer, METRICS,
                                     scheduler, analyser)
        logging.info('train_acc %f', train_acc)

        # validation
        valid_acc, valid_obj = infer(valid_queue, model, criterion)
        logging.info('valid_acc %f', valid_acc)

        # metrics
        io_metrics = METRICS.evaluate(epoch)
        PERFORMANCE_STATISTICS[
            f'in_S_epoch_{epoch}'] = io_metrics.input_channel_S
        PERFORMANCE_STATISTICS[
            f'out_S_epoch_{epoch}'] = io_metrics.output_channel_S
        PERFORMANCE_STATISTICS[f'fc_S_epoch_{epoch}'] = io_metrics.fc_S
        PERFORMANCE_STATISTICS[
            f'in_rank_epoch_{epoch}'] = io_metrics.input_channel_rank
        PERFORMANCE_STATISTICS[
            f'out_rank_epoch_{epoch}'] = io_metrics.output_channel_rank
        PERFORMANCE_STATISTICS[f'fc_rank_epoch_{epoch}'] = io_metrics.fc_rank
        PERFORMANCE_STATISTICS[
            f'in_condition_epoch_{epoch}'] = io_metrics.input_channel_condition
        PERFORMANCE_STATISTICS[
            f'out_condition_epoch_{epoch}'] = io_metrics.output_channel_condition
        ################################################################################
        # AdaS: update learning rates
        lr_metrics = scheduler.step(epoch, METRICS)
        PERFORMANCE_STATISTICS[
            f'rank_velocity_epoch_{epoch}'] = lr_metrics.rank_velocity
        PERFORMANCE_STATISTICS[
            f'learning_rate_epoch_{epoch}'] = lr_metrics.r_conv
        ################################################################################
        # write metrics data to xls file
        metrics_df = pd.DataFrame(data=PERFORMANCE_STATISTICS)
        metrics_df.to_excel(metrics_path)

        # weights
        weights_normal = F.softmax(model.alphas_normal,
                                   dim=-1).detach().cpu().numpy()
        weights_reduce = F.softmax(model.alphas_reduce,
                                   dim=-1).detach().cpu().numpy()
        # normal
        ARCH_STATISTICS[f'normal_none_epoch{epoch}'] = weights_normal[:, 0]
        ARCH_STATISTICS[f'normal_max_epoch{epoch}'] = weights_normal[:, 1]
        ARCH_STATISTICS[f'normal_avg_epoch{epoch}'] = weights_normal[:, 2]
        ARCH_STATISTICS[f'normal_skip_epoch{epoch}'] = weights_normal[:, 3]
        ARCH_STATISTICS[f'normal_sep_3_epoch{epoch}'] = weights_normal[:, 4]
        ARCH_STATISTICS[f'normal_sep_5_epoch{epoch}'] = weights_normal[:, 5]
        ARCH_STATISTICS[f'normal_dil_3_epoch{epoch}'] = weights_normal[:, 6]
        ARCH_STATISTICS[f'normal_dil_5_epoch{epoch}'] = weights_normal[:, 7]
        # reduce
        ARCH_STATISTICS[f'reduce_none_epoch{epoch}'] = weights_reduce[:, 0]
        ARCH_STATISTICS[f'reduce_max_epoch{epoch}'] = weights_reduce[:, 1]
        ARCH_STATISTICS[f'reduce_avg_epoch{epoch}'] = weights_reduce[:, 2]
        ARCH_STATISTICS[f'reduce_skip_epoch{epoch}'] = weights_reduce[:, 3]
        ARCH_STATISTICS[f'reduce_sep_3_epoch{epoch}'] = weights_reduce[:, 4]
        ARCH_STATISTICS[f'reduce_sep_5_epoch{epoch}'] = weights_reduce[:, 5]
        ARCH_STATISTICS[f'reduce_dil_3_epoch{epoch}'] = weights_reduce[:, 6]
        ARCH_STATISTICS[f'reduce_dil_5_epoch{epoch}'] = weights_reduce[:, 7]
        # write weights data to xls file
        weights_df = pd.DataFrame(data=ARCH_STATISTICS)
        weights_df.to_excel(weights_path)

        # adaptive stopping criterion
        if args.adaptive_stop and epoch >= 10:
            # apply local stopping criterion
            stop_checker.local_stop(METRICS, epoch)
            # freeze some edges based on their knowledge gains
            iteration_p = 0
            for p in model.parameters():
                if ~METRICS.layers_index_todo[iteration_p]:
                    p.requires_grad = False
                    p.grad = None
                iteration_p += 1

        utils.save(model, os.path.join(args.save, 'weights.pt'))
Beispiel #38
0
def main():
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)
    if args.set == 'cifar100':
        N_CLASSES = 100
    if args.set == 'tiny_imagenet':
        N_CLASSES = 200
    else:
        N_CLASSES = 10

    config = QuantumFold_config(None, 0)
    if config.op_struc == "": args.batch_size = args.batch_size // 4
    config.exp_dir = args.save
    config.primitive = args.primitive
    config.attention = args.attention
    config.device = OnInitInstance(args.seed, args.gpu)
    if config.primitive == "p0":
        config.PRIMITIVES_pool = [
            'none', 'max_pool_3x3', 'avg_pool_3x3', 'Identity', 'BatchNorm2d',
            'ReLU', 'Conv_3', 'Conv_5'
        ]
    elif config.primitive == "p1":
        config.PRIMITIVES_pool = [
            'none', 'max_pool_3x3', 'skip_connect', 'BatchNorm2d', 'ReLU',
            'Conv_3', 'DepthConv_3', 'Conv_11'
        ]
    elif config.primitive == "p2" or config.primitive == "p21":
        config.PRIMITIVES_pool = [
            'none', 'max_pool_3x3', 'skip_connect', 'BatchNorm2d', 'ReLU',
            'Conv_3', 'DepthConv_3', 'Conv_11'
        ]
    elif config.primitive == "p3":
        config.PRIMITIVES_pool = [
            'none', 'max_pool_3x3', 'max_pool_5x5', 'skip_connect', 'Identity',
            'BatchNorm2d', 'ReLU', 'Conv_3', 'DepthConv_3', 'Conv_5',
            'DepthConv_5', 'Conv_11', 'sep_conv_3x3'
        ]
    elif config.primitive == "c0":
        config.PRIMITIVES_pool = [
            'none', 'max_pool_3x3', 'avg_pool_3x3', 'skip_connect',
            'sep_conv_3x3', 'sep_conv_5x5', 'dil_conv_3x3', 'dil_conv_5x5'
        ]
    #args.load_workers = 8

    np.random.seed(args.seed)
    torch.cuda.set_device(args.gpu)
    cudnn.benchmark = True
    torch.manual_seed(args.seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(args.seed)
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)

    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    model = Network(config, args.init_channels, N_CLASSES, args.layers,
                    criterion)
    print(model)
    #dump_model_params(model)
    model = model.cuda()
    model.visual = Visdom_Visualizer(
        env_title=f"{args.set}_{model.title}_{args.legend}")
    model.visual.img_dir = "./results/images/"
    logging.info("param size = %.3fMB", utils.count_parameters_in_MB(model))

    optimizer = torch.optim.SGD(model.parameters(),
                                args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    if args.set == 'cifar100':
        N_CLASSES = 100
        train_transform, valid_transform = utils._data_transforms_cifar10(args)
        train_data = dset.CIFAR100(root=args.data,
                                   train=True,
                                   download=True,
                                   transform=train_transform)
    elif args.set == 'tiny_imagenet':
        N_CLASSES = 200
        train_data = TinyImageNet200(root=args.data, train=True, download=True)
        infer_data = TinyImageNet200(root=args.data,
                                     train=False,
                                     download=True)
    else:
        train_transform, valid_transform = utils._data_transforms_cifar10(args)
        train_data = dset.CIFAR10(root=args.data,
                                  train=True,
                                  download=True,
                                  transform=train_transform)
        infer_data = dset.CIFAR10(root=args.data,
                                  train=False,
                                  download=True,
                                  transform=valid_transform)  #这个更合理

    num_train = len(train_data)
    indices = list(range(num_train))
    split = int(np.floor(args.train_portion * num_train))

    train_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
        pin_memory=True,
        num_workers=args.load_workers)  #args.load_workers

    valid_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(
            indices[split:num_train]),
        pin_memory=True,
        num_workers=0)
    if True:
        infer_queue = torch.utils.data.DataLoader(infer_data,
                                                  batch_size=args.batch_size,
                                                  shuffle=False,
                                                  pin_memory=True,
                                                  num_workers=0)
    else:
        infer_queue = valid_queue

    config.experiment = Experiment(config,
                                   "cifar_10",
                                   model,
                                   loss_fn=None,
                                   optimizer=optimizer,
                                   objective_metric=None)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs), eta_min=args.learning_rate_min)

    architect = Architect(model, args)
    #model.init_on_data(valid_queue,criterion)
    architect.init_on_data(valid_queue, criterion)  #data-aware init

    print(architect)

    print(f"======\tconfig={config.__dict__}\n")
    print(f"======\targs={args.__dict__}\n")
    valid_acc, t0 = 0, time.time()
    for epoch in range(args.epochs):
        scheduler.step()
        plot_path = f"{model.config.exp_dir}/{model.title}E{epoch}_a{valid_acc:.1f}_"
        dump_genotype(model, logging, plot_path)
        lr = scheduler.get_lr()[0]
        logging.info('epoch=%d lr=%e', epoch, lr)
        print(
            f"======\tnTrain={train_queue.dataset.__len__()} nSearch={valid_queue.dataset.__len__()} nTest={infer_queue.dataset.__len__()} "
        )
        # plot_path=f"{model.config.exp_dir}/{model.title}E{epoch}_a{valid_acc:.1f}_"
        # dump_genotype(model,logging,plot_path)

        # training
        train_acc, train_obj = train(train_queue, valid_queue, model,
                                     architect, criterion, optimizer, lr,
                                     epoch)
        logging.info(f'train_acc {train_acc} T={time.time()-t0:.2f}')

        # validation
        valid_acc, valid_obj = infer(infer_queue, model, criterion, epoch)
        logging.info(f'valid_acc {valid_acc} T={time.time()-t0:.2f}')
        config.experiment.best_score = max(valid_acc,
                                           config.experiment.best_score)
        utils.save(model, os.path.join(args.save, 'weights.pt'))
        model.visual.UpdateLoss(title=f"Accuracy on \"{args.set}\"",
                                legend=f"{model.title}",
                                loss=valid_acc,
                                yLabel="Accuracy")
Beispiel #39
0
 def save_image(self):
     itm = self.wrk.get()
     url = itm['img_url']
     data = itm['img_orig']
     utils.save(data, url)
Beispiel #40
0
def main():


    np.random.seed(args.seed)
    torch.cuda.set_device(args.gpu)
    cudnn.benchmark = True
    cudnn.enabled = True
    torch.manual_seed(args.seed)
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)


    if not args.arch:
        geno_s = pathlib.Path(geno_path).read_text()
    else:
        geno_s = "genotypes.%s" % args.arch
    genotype = eval(geno_s)
    model = Network(args.init_ch, 10, args.layers, args.auxiliary, genotype).cuda()

    logging.info(f"seed = {args.seed}")
    logging.info(f"geno_s = {geno_s}")
    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    criterion = nn.CrossEntropyLoss().cuda()
    optimizer = torch.optim.SGD(
        model.parameters(),
        args.lr,
        momentum=args.momentum,
        weight_decay=args.wd
    )

    train_transform, valid_transform = utils._data_transforms_cifar10(args)
    train_data = dset.CIFAR10(root=args.data, train=True, download=True, transform=train_transform)
    valid_data = dset.CIFAR10(root=args.data, train=False, download=True, transform=valid_transform)

    train_queue = torch.utils.data.DataLoader(
        train_data, batch_size=args.batchsz, shuffle=True, pin_memory=True, num_workers=0 if 'pydevd' in sys.modules else 2)

    valid_queue = torch.utils.data.DataLoader(
        valid_data, batch_size=args.batchsz, shuffle=False, pin_memory=True, num_workers=0 if 'pydevd' in sys.modules else 2)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, float(args.epochs))

    lines = [f'epoch\ttrain_acc\tval_acc']
    for epoch in range(args.epochs):
        scheduler.step()
        logging.info('epoch %d lr %e', epoch, scheduler.get_lr()[0])
        model.drop_path_prob = args.drop_path_prob * epoch / args.epochs

        train_acc, train_obj = train(train_queue, model, criterion, optimizer)
        logging.info('train_acc: %f', train_acc)

        valid_acc, valid_obj = infer(valid_queue, model, criterion)
        logging.info('valid_acc: %f', valid_acc)

        lines.append(f'{epoch}\t{train_acc}\t{valid_acc}')

    timebudget.report()
    utils.save(model, os.path.join(args.save, 'trained.pt'))
    print('saved to: trained.pt')
    pathlib.Path(os.path.join(args.exp_path, 'eval.tsv')).write_text('\n'.join(lines))
Beispiel #41
0
def main():

    np.random.seed(args.seed)
    random.seed(args.seed)

    if torch.cuda.is_available():
        device = torch.device('cuda:{}'.format(args.gpu))
        cudnn.benchmark = False
        torch.manual_seed(args.seed)
        cudnn.enabled = True
        cudnn.deterministic = True
        torch.cuda.manual_seed(args.seed)
        logging.info('gpu device = %d' % args.gpu)
    else:
        device = torch.device('cpu')
        logging.info('No gpu device available')
        torch.manual_seed(args.seed)

    logging.info("args = %s", args)
    genotype = eval("genotypes.%s" % args.arch)
    model = Network(args.init_channels, CIFAR_CLASSES, args.layers,
                    args.auxiliary, genotype)
    model = model.to(device)

    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))
    total_params = sum(x.data.nelement() for x in model.parameters())
    logging.info('Model total parameters: {}'.format(total_params))

    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    optimizer = torch.optim.SGD(model.parameters(),
                                args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    train_transform, valid_transform = utils._data_transforms_cifar10(
        args.cutout, args.cutout_length)
    train_data = dset.CIFAR10(root=args.data,
                              train=True,
                              download=True,
                              transform=train_transform)
    valid_data = dset.CIFAR10(root=args.data,
                              train=False,
                              download=True,
                              transform=valid_transform)

    train_queue = torch.utils.data.DataLoader(train_data,
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              pin_memory=True,
                                              num_workers=0)

    valid_queue = torch.utils.data.DataLoader(valid_data,
                                              batch_size=args.batch_size // 2,
                                              shuffle=False,
                                              pin_memory=True,
                                              num_workers=0)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs))

    for epoch in range(args.epochs):

        logging.info('epoch %d lr %e', epoch, scheduler.get_lr()[0])
        model.drop_path_prob = args.drop_path_prob * epoch / args.epochs

        train_acc, train_obj = train(train_queue, model, criterion, optimizer,
                                     args.gpu)
        logging.info('train_acc %f', train_acc)

        valid_acc, valid_obj = infer(valid_queue, model, criterion, args.gpu)
        logging.info('valid_acc %f', valid_acc)

        scheduler.step()

        utils.save(model, os.path.join(args.save, 'weights.pt'))
def train(directories, args):  # args:所有的参数解析
    logdir = directories['logdir']
    print("logdir:", logdir)
    restore_from = directories['restore_from']

    # Even if we restored the model, we will treat it as new training
    # if the trained model is written into a location that's different from logdir.
    is_overwritten_training = logdir != restore_from  # 先算后部分,返回bool值. 结果:False,意思是两个文件夹相同

    coord = tf.train.Coordinator()  # 创建一个协调器,管理线程
    # create inputs
    gc_enabled = args.gc_channels is not None
    reader = AudioReader(args.data_dir,
                         args.test_dir,
                         coord,
                         sample_rate=args.sample_rate,
                         gc_enabled=gc_enabled)
    audio_batch = reader.dequeue(args.batch_size)

    # Initialize model
    net = SpeechSeparation(
        batch_size=args.batch_size,
        rnn_type=args.rnn_type,
        dim=args.dim,
        n_rnn=args.n_rnn,
        seq_len=args.seq_len,
        num_of_frequency_points=args.num_of_frequency_points)

    # need to modify net to include these
    #out =
    summary, output1, output2, losses, apply_gradient_op = net.initializer(
        net, args)  # output1/2:(1,256,257)
    speech_inputs_1 = net.speech_inputs_1  # (1,256,257)
    speech_inputs_2 = net.speech_inputs_2  # (1,256,257)
    speech_inputs_mix = net.speech_inputs_mix  # (1,256,257)

    # Set up session
    tf_config = tf.ConfigProto(
        # allow_soft_placement is set to True to build towers on GPU
        allow_soft_placement=True,
        log_device_placement=False,
        inter_op_parallelism_threads=1)
    tf_config.gpu_options.allow_growth = True
    sess = tf.Session(config=tf_config)

    sess.run(tf.global_variables_initializer())
    # Create coordinator.

    # Set up logging for TensorBoard.
    writer = tf.summary.FileWriter(logdir)
    writer.add_graph(tf.get_default_graph())
    run_metadata = tf.RunMetadata()  # 定义TensorFlow运行元信息,记录训练运算时间和内存占用等信息

    # Saver for storing checkpoints of the model.
    saver = tf.train.Saver(var_list=tf.trainable_variables(),
                           max_to_keep=args.max_checkpoints)

    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    reader.start_threads(sess)
    try:
        saved_global_step = load(saver, sess, restore_from)  # 第一次结果:None
        if is_overwritten_training or saved_global_step is None:
            # The first training step will be saved_global_step + 1,
            # therefore we put -1 here for new or overwritten trainings.
            saved_global_step = -1
    except:
        print(
            "Something went wrong while restoring checkpoint. We will terminate "
            "training to avoid accidentally overwriting the previous model.")
        raise


#################################Start Training####################################
    last_saved_step = saved_global_step  # -1
    try:
        for step in range(saved_global_step + 1,
                          args.num_steps):  # (0, 1000000)
            loss_sum = 0
            start_time = time.time()

            inputslist = [sess.run(audio_batch) for i in range(args.num_gpus)
                          ]  # len:1, 里边还有四维,每一维(1,697,257),括号中1的个数指gpu个数
            inp_dict = create_inputdict(inputslist, args, speech_inputs_1,
                                        speech_inputs_2, speech_inputs_mix)
            summ, loss_value, _ = sess.run(
                [summary, losses, apply_gradient_op],
                feed_dict=inp_dict)  #feed_dict前一个数是占位符,后一个是真实值

            for g in range(args.num_gpus):
                loss_sum += loss_value[g] / args.num_gpus

            writer.add_summary(summ, step)
            duration = time.time() - start_time

            if (step < 100):
                log_str = ('step {%d} - loss = {%0.3f}, ({%0.3f} sec/step') % (
                    step, loss_sum, duration)
                logging.warning(log_str)

            elif (0 == step % 100):
                log_str = ('step {%d} - loss = {%0.3f}, ({%0.3f} sec/step') % (
                    step, loss_sum / 100, duration)
                logging.warning(log_str)

            if (0 == step % 2000):
                angle_test, inp_dict = create_inputdict(inputslist,
                                                        args,
                                                        speech_inputs_1,
                                                        speech_inputs_2,
                                                        speech_inputs_mix,
                                                        test=True)

                outp1, outp2 = sess.run([output1, output2], feed_dict=inp_dict)
                x_r = mk_audio(outp1, angle_test, args.sample_rate,
                               "spk1_test_" + str(step) + ".wav")
                y_r = mk_audio(outp2, angle_test, args.sample_rate,
                               "spk2_test_" + str(step) + ".wav")

                amplitude_test = inputslist[0][2]
                angle_test = inputslist[0][3]
                mk_audio(amplitude_test, angle_test, args.sample_rate,
                         "raw_test_" + str(step) + ".wav")

                # audio summary on tensorboard
                merged = sess.run(
                    tf.summary.merge([
                        tf.summary.audio('speaker1_' + str(step),
                                         x_r[None, :],
                                         args.sample_rate,
                                         max_outputs=1),
                        tf.summary.audio('speaker2_' + str(step),
                                         y_r[None, :],
                                         args.sample_rate,
                                         max_outputs=1)
                    ]))
                writer.add_summary(merged, step)

            if step % args.checkpoint_every == 0:
                save(saver, sess, logdir, step)
                last_saved_step = step

    except KeyboardInterrupt:
        # Introduce a line break after ^C is displayed so save message is on its own line.
        print()
    finally:
        #'''
        if step > last_saved_step:
            save(saver, sess, logdir, step)
        #'''
        coord.request_stop()
        coord.join(threads)
Beispiel #43
0
def main():
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)
    np.random.seed(args.seed)
    torch.cuda.set_device(args.gpu)
    cudnn.benchmark = True
    torch.manual_seed(args.seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(args.seed)
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)
    face_factor, bbox_reg_factor, rig_factor = 1, 0.8, 1

    criterion = LossFn_3(face_factor=face_factor,
                         box_factor=bbox_reg_factor, rig_factor=rig_factor)
    model = SACN_3(args.init_channels, args.layers, criterion)

    criterion = criterion.cuda()
    model = model.cuda()
    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    optimizer = torch.optim.SGD(
        model.parameters(),
        args.learning_rate,
        momentum=args.momentum,
        weight_decay=args.weight_decay)

    train_transform, valid_transform = utils._data_transforms_Wider(args)
    train_data = dset.wider(
        anno_file="../anno_store/imglist_anno_48_45.txt", train=True, transform=train_transform)
    valid_data = dset.wider(
        anno_file="../anno_store/imglist_anno_48_45.txt", train=False, transform=valid_transform)
    num_train = len(train_data)
    indices = list(range(num_train))
    split = int(np.floor(args.train_portion * num_train))

    train_queue = torch.utils.data.DataLoader(
        train_data, batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
        pin_memory=True, num_workers=2)

    search_queue = torch.utils.data.DataLoader(
        train_data, batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(
            indices[split:num_train]),
        pin_memory=True, num_workers=2)

    valid_queue = torch.utils.data.DataLoader(
        valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=2)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs), eta_min=args.learning_rate_min)

    architect = Architect_SACN_3(model, args)

    for epoch in range(args.epochs):
        scheduler.step()
        lr = scheduler.get_lr()[0]
        logging.info('epoch %d lr %e', epoch, lr)

        genotype = model.genotype()
        logging.info('genotype = %s', genotype)

        print(F.softmax(model.alphas_normal, dim=-1))
        print(F.softmax(model.alphas_reduce, dim=-1))

        # training
        train_acc, train_rig, train_bbx, train_obj, arch_grad_norm = train(
            train_queue, search_queue, model, architect, criterion, optimizer, lr)
        logging.info('train_label_acc %f rig %f bbx %f',
                     train_acc, train_rig, train_bbx)

        # validation
        valid_acc, valid_rig, valid_bbx, valid_obj = infer(
            valid_queue, model, criterion)
        logging.info('valid_label_acc %f rig %f bbx %f',
                     valid_acc, valid_rig, valid_bbx)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
Beispiel #44
0
        training_statistic.append(res)

        test_source = test(model, source_loader, e)
        test_target = test(model, target_loader, e)
        testing_s_statistic.append(test_source)
        testing_t_statistic.append(test_target)

        print(
            '###Test Source: Epoch: {}, avg_loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'
            .format(
                e + 1,
                test_source['average_loss'],
                test_source['correct'],
                test_source['total'],
                test_source['accuracy'],
            ))
        print(
            '###Test Target: Epoch: {}, avg_loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'
            .format(
                e + 1,
                test_target['average_loss'],
                test_target['correct'],
                test_target['total'],
                test_target['accuracy'],
            ))

    utils.save(training_statistic, 'training_statistic.pkl')
    utils.save(testing_s_statistic, 'testing_s_statistic.pkl')
    utils.save(testing_t_statistic, 'testing_t_statistic.pkl')
    utils.save_net(model, 'checkpoint.tar')
Beispiel #45
0
def train(args):
    """
        Train a VQG model using the training set and validate on val set.
    """

    # Load the VQA training set
    print('Loading data...')
    dataset = VQA_Dataset(args.data_dir, args.emb)
    loader = DataLoader(dataset,
                        batch_size=args.bsize,
                        shuffle=True,
                        num_workers=0,
                        collate_fn=collate_fn)

    # Load the VQA validation set
    dataset_test = VQA_Dataset(args.data_dir, args.emb, train=False)
    loader_val = DataLoader(dataset_test,
                            batch_size=args.bsize,
                            shuffle=False,
                            num_workers=0,
                            collate_fn=collate_fn)

    n_batches = len(dataset) // args.bsize
    question_vocab = pickle.load(
        open('/mnt/data/xiaojinhui/wangtan_MM/vqa-project/data/train_q_dict.p',
             'rb'))

    # Print data and model parameters
    print('Parameters:\n\t'
          'vocab size: %d\n\tembedding dim: %d\n\tfeature dim: %d'
          '\n\thidden dim: %d\n\toutput dim: %d' %
          (dataset.n_answers, args.emb, dataset.feat_dim, args.hid,
           dataset.n_answers))

    print('Initializing model')
    model_gcn = conditional_GCN(nfeat=options['gcn']['nfeat'],
                                nhid=options['gcn']['nhid'],
                                nclass=options['gcn']['nclass'],
                                emb=options['gcn']['fliter_emb'],
                                dropout=options['gcn']['dropout'])
    # model_gcn_nofinding = layer_vqg.conditional_GCN_1(nfeat=options['gcn']['nfeat'],
    #                             nhid=options['gcn']['nhid'],
    #                             nclass=options['gcn']['nclass'],
    #                             emb = options['gcn']['fliter_emb'],
    #                             dropout=options['gcn']['dropout'])
    model_vqg = question_gen(vocab=question_vocab['wtoi'],
                             vocab_i2t=question_vocab['itow'],
                             opt=options['vqg'])
    # no_finding = layer_vqg.no_finding_area_top(in_feature=2652, hidden_feature=512, dropout=options['gcn']['dropout'])

    criterion = nn.CrossEntropyLoss()

    # Move it to GPU
    model_gcn = model_gcn.cuda()
    model_vqg = model_vqg.cuda()
    # model_gcn_nofinding = model_gcn_nofinding.cuda()
    # no_finding = no_finding.cuda()
    criterion = criterion.cuda()

    # Define the optimiser
    optimizer = torch.optim.Adam([{
        'params': model_gcn.parameters()
    }, {
        'params': model_vqg.parameters()
    }],
                                 lr=args.lr)

    # Continue training from saved model
    start_ep = 0
    if args.model_path and os.path.isfile(args.model_path):
        print('Resuming from checkpoint %s' % (args.model_path))
        ckpt = torch.load(args.model_path)
        start_ep = ckpt['epoch']
        model_gcn.load_state_dict(ckpt['state_dict_gcn'])
        model_vqg.load_state_dict(ckpt['state_dict_vqg'])
        optimizer.load_state_dict(ckpt['optimizer'])

    # Update the learning rate
    for param_group in optimizer.param_groups:
        param_group['lr'] = args.lr

    # Learning rate scheduler
    scheduler = MultiStepLR(optimizer, milestones=[30], gamma=0.5)
    scheduler.last_epoch = start_ep - 1

    # Train iterations
    print('Start training.')
    bleu_best = [0.0, 0.0, 0.0, 0.0]
    cider_best = 0.0
    meteor_best = 0.0
    rouge_best = 0.0
    for ep in range(start_ep, start_ep + args.ep):

        adjust_learning_rate(optimizer, ep)
        scheduler.step()
        ep_loss = 0.0
        ep_top3 = 0.0
        ep_top1 = 0.0
        ave_loss = 0.0
        ave_top3 = 0.0
        ave_top1 = 0.0
        iter_time_all = 0.0

        for step, next_batch in enumerate(loader):

            model_gcn.train()
            model_vqg.train()
            # Move batch to cuda
            target_q, an_feat, img_feat, adj_mat = \
                utils.batch_to_cuda(next_batch, volatile=True)

            # forward pass
            torch.cuda.synchronize()
            start = time.time()

            # img_feat_no = torch.mul(img_feat[:,:,None,:], img_feat[:,None,:,:]).view(-1, 2652)
            # adj_mat = no_finding(img_feat_no).view(-1,36,36)
            # adj_mat += torch.eye(36).cuda()
            # adj_mat = torch.clamp(adj_mat, max=1)
            feat_gcn, adj_new = model_gcn(img_feat, adj_mat)
            # feat_gcn, adj_new = model_gcn_nofinding(img_feat, adj_mat)
            output = model_vqg(feat_gcn, an_feat, target_q)
            # for i in range(256):
            #     dataset.drawarea[i]['adj'] = adj_new[i].detach().cpu().numpy().tolist()
            #     dataset.drawarea[i]['adj_diag'] = np.diag(adj_new[i].detach().cpu().numpy()).tolist()
            #
            # json.dump(dataset.drawarea, open('/mnt/data/xiaojinhui/wangtan_MM/vqa-project/draw/new_adj_t.json', 'w'))
            # output_bs = model_vqg.beam_search(feat_gcn[0].unsqueeze(0),
            #                                   an_feat[0].unsqueeze(0))
            target_q = target_q[:, 1:].contiguous()

            loss = criterion(
                output.view(output.size(0) * output.size(1), output.size(2)),
                target_q.view(target_q.size(0) * target_q.size(1)))

            # Compute batch accu

            top1 = utils.accuracy(output, target_q, 1)
            top3 = utils.accuracy(output, target_q, 3)

            ep_top1 += top1
            ep_top3 += top3
            ep_loss += loss.item()
            ave_top1 += top1
            ave_top3 += top3
            ave_loss += loss.item()

            # This is a 40 step average
            if step % 40 == 0 and step != 0:
                print(
                    '  Epoch %02d(%03d/%03d), ave loss: %.7f, top1: %.2f%%, top3: %.2f%%, iter time: %.4fs'
                    % (ep + 1, step, n_batches, ave_loss / 40, ave_top1 / 40,
                       ave_top3 / 40, iter_time_all / 40))

                ave_top1 = 0
                ave_top3 = 0
                ave_loss = 0
                iter_time_all = 0

            # Compute gradient and do optimisation step
            optimizer.zero_grad()
            loss.backward()
            # clip_grad_norm_(model_gcn.parameters(), 2.)
            # clip_grad_norm_(model_gcn.parameters(), 2.)
            # clip_grad_norm_(no_finding.parameters(), 2.)
            optimizer.step()

            end = time.time()
            iter_time = end - start
            iter_time_all += iter_time

            # save model and compute validation accuracy every 400 steps
            if step == 0:
                with torch.no_grad():
                    epoch_loss = ep_loss / n_batches
                    epoch_top1 = ep_top1 / n_batches
                    epoch_top3 = ep_top3 / n_batches

                    # compute validation accuracy over a small subset of the validation set
                    model_gcn.train(False)
                    model_vqg.train(False)
                    model_gcn.eval()
                    model_vqg.eval()

                    output_all = []
                    output_all_bs = {}
                    ref_all = []

                    flag_val = 0

                    for valstep, val_batch in tqdm(enumerate(loader_val)):
                        # test_batch = next(loader_test)
                        target_q, an_feat, img_feat, adj_mat = \
                            utils.batch_to_cuda(val_batch, volatile=True)
                        # img_feat_no = torch.mul(img_feat[:, :, None, :], img_feat[:, None, :, :]).view(-1, 2652)
                        # adj_mat = no_finding(img_feat_no).view(-1, 36, 36)
                        # adj_mat += torch.eye(36).cuda()
                        # adj_mat = torch.clamp(adj_mat, max=1)
                        # feat_gcn, _ = model_gcn_nofinding(img_feat, adj_mat)
                        feat_gcn, adj_new = model_gcn(img_feat, adj_mat)
                        output = model_vqg.generate(feat_gcn, an_feat)

                        for j in range(feat_gcn.size(0)):
                            output_bs = model_vqg.beam_search(
                                feat_gcn[j].unsqueeze(0),
                                an_feat[j].unsqueeze(0))
                            output_all_bs[flag_val] = output_bs
                            flag_val += 1

                        output_all.append(output.cpu().numpy())
                        ref_all.append(target_q[:, :-1].cpu().numpy())

                    gen, ref = utils.idx2question(
                        np.concatenate(output_all, 0),
                        np.concatenate(ref_all, 0), question_vocab['itow'])
                    print(gen.values()[:10])

                    # save the best
                    bleu, cider, meteor, rouge = main.main(ref, gen)
                    bleu_best, cider_best, meteor_best, rouge_best, choice = utils.save_the_best(
                        bleu, cider, meteor, rouge, bleu_best, cider_best,
                        meteor_best, rouge_best)
                    if choice:
                        utils.save(model_gcn,
                                   model_vqg,
                                   optimizer,
                                   ep,
                                   epoch_loss,
                                   epoch_top1,
                                   dir=args.save_dir,
                                   name=args.name + '_' + str(ep + 1))

                    print('use beam search...')
                    bleu, cider, meteor, rouge = main.main(ref, output_all_bs)
                    bleu_best, cider_best, meteor_best, rouge_best, choice = utils.save_the_best(
                        bleu, cider, meteor, rouge, bleu_best, cider_best,
                        meteor_best, rouge_best)
                    if choice:
                        utils.save(model_gcn,
                                   model_vqg,
                                   optimizer,
                                   ep,
                                   epoch_loss,
                                   epoch_top1,
                                   dir=args.save_dir,
                                   name=args.name + '_' + str(ep + 1))

                    print(
                        'the best bleu: %s, cider: %.6s, meteor: %.6s, rouge: %.6s'
                        % (bleu_best, cider_best, meteor_best, rouge_best))
                    print(output_all_bs.values()[:10])

                model_gcn.train(True)
                model_vqg.train(True)
Beispiel #46
0
    def run(self):
        g.init()
        if not self.journal: utils.load()
        load_save.retrieve()
        self.som4 = som.Som(4)
        self.som5 = som.Som(5)
        self.som4.setup()
        self.som5.setup()
        if g.n == 4: self.som = self.som4
        else: self.som = self.som5
        self.som4.found = g.found4
        self.som5.found = g.found5
        self.buttons_setup()
        if self.canvas <> None: self.canvas.grab_focus()
        ctrl = False
        pygame.key.set_repeat(600, 120)
        key_ms = pygame.time.get_ticks()
        going = True
        while going:
            if self.journal:
                # Pump GTK messages.
                while gtk.events_pending():
                    gtk.main_iteration()

            # Pump PyGame messages.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    if not self.journal: utils.save()
                    going = False
                elif event.type == pygame.MOUSEMOTION:
                    g.pos = event.pos
                    g.redraw = True
                    if self.canvas <> None: self.canvas.grab_focus()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    g.redraw = True
                    if event.button == 1:
                        if self.do_click():
                            pass
                        else:
                            bu = buttons.check()
                            if bu != '':
                                self.do_button(bu)
                                self.flush_queue()
                    if event.button == 3:
                        self.som.right_click()
                        self.flush_queue()
                elif event.type == pygame.KEYDOWN:
                    # throttle keyboard repeat
                    if pygame.time.get_ticks() - key_ms > 110:
                        key_ms = pygame.time.get_ticks()
                        if ctrl:
                            if event.key == pygame.K_q:
                                if not self.journal: utils.save()
                                going = False
                                break
                            else:
                                ctrl = False
                        if event.key in (pygame.K_LCTRL, pygame.K_RCTRL):
                            ctrl = True
                            break
                        self.do_key(event.key)
                        g.redraw = True
                        self.flush_queue()
                elif event.type == pygame.KEYUP:
                    ctrl = False
            if not going: break
            if g.redraw:
                self.display()
                if g.version_display: utils.version_display()
                g.screen.blit(g.pointer, g.pos)
                pygame.display.flip()
                g.redraw = False
            g.clock.tick(40)
Beispiel #47
0
def main():
    if not torch.cuda.is_available():
        logging.info('No GPU device available')
        sys.exit(1)
    np.random.seed(args.seed)
    cudnn.benchmark = True
    torch.manual_seed(args.seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(args.seed)
    logging.info("args = %s", args)
    #  prepare dataset
    if args.cifar100:
        train_transform, valid_transform = utils._data_transforms_cifar100(
            args)
    else:
        train_transform, valid_transform = utils._data_transforms_cifar10(args)
    if args.cifar100:
        train_data = dset.CIFAR100(root=args.tmp_data_dir,
                                   train=True,
                                   download=True,
                                   transform=train_transform)
    else:
        train_data = dset.CIFAR10(root=args.tmp_data_dir,
                                  train=True,
                                  download=True,
                                  transform=train_transform)

    num_train = len(train_data)
    indices = list(range(num_train))
    split = int(np.floor(args.train_portion * num_train))

    train_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
        pin_memory=True,
        num_workers=args.workers)

    valid_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(
            indices[split:num_train]),
        pin_memory=True,
        num_workers=args.workers)

    # build Network
    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    switches = []
    for i in range(14):
        switches.append([True for j in range(len(PRIMITIVES))])
    switches_normal = copy.deepcopy(switches)
    switches_reduce = copy.deepcopy(switches)
    # To be moved to args
    num_to_keep = [5, 3, 1]
    num_to_drop = [3, 2, 2]
    if len(args.add_width) == 3:
        add_width = args.add_width
    else:
        add_width = [0, 0, 0]
    if len(args.add_layers) == 3:
        add_layers = args.add_layers
    else:
        add_layers = [0, 6, 12]
    if len(args.dropout_rate) == 3:
        drop_rate = args.dropout_rate
    else:
        drop_rate = [0.0, 0.0, 0.0]
    eps_no_archs = [10, 10, 10]
    for sp in range(len(num_to_keep)):
        model = Network(args.init_channels + int(add_width[sp]),
                        CIFAR_CLASSES,
                        args.layers + int(add_layers[sp]),
                        criterion,
                        switches_normal=switches_normal,
                        switches_reduce=switches_reduce,
                        p=float(drop_rate[sp]))
        model = nn.DataParallel(model)
        model = model.cuda()
        logging.info("param size = %fMB", utils.count_parameters_in_MB(model))
        network_params = []
        for k, v in model.named_parameters():
            if not (k.endswith('alphas_normal')
                    or k.endswith('alphas_reduce')):
                network_params.append(v)
        optimizer = torch.optim.SGD(network_params,
                                    args.learning_rate,
                                    momentum=args.momentum,
                                    weight_decay=args.weight_decay)
        optimizer_a = torch.optim.Adam(model.module.arch_parameters(),
                                       lr=args.arch_learning_rate,
                                       betas=(0.5, 0.999),
                                       weight_decay=args.arch_weight_decay)
        scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
            optimizer, float(args.epochs), eta_min=args.learning_rate_min)
        sm_dim = -1
        epochs = args.epochs
        eps_no_arch = eps_no_archs[sp]
        scale_factor = 0.2
        for epoch in range(epochs):
            scheduler.step()
            lr = scheduler.get_lr()[0]
            logging.info('Epoch: %d lr: %e', epoch, lr)
            epoch_start = time.time()
            # training
            if epoch < eps_no_arch:
                model.module.p = float(
                    drop_rate[sp]) * (epochs - epoch - 1) / epochs
                model.module.update_p()
                train_acc, train_obj = train(train_queue,
                                             valid_queue,
                                             model,
                                             network_params,
                                             criterion,
                                             optimizer,
                                             optimizer_a,
                                             lr,
                                             train_arch=False)
            else:
                model.module.p = float(drop_rate[sp]) * np.exp(
                    -(epoch - eps_no_arch) * scale_factor)
                model.module.update_p()
                train_acc, train_obj = train(train_queue,
                                             valid_queue,
                                             model,
                                             network_params,
                                             criterion,
                                             optimizer,
                                             optimizer_a,
                                             lr,
                                             train_arch=True)
            logging.info('Train_acc %f', train_acc)
            epoch_duration = time.time() - epoch_start
            logging.info('Epoch time: %ds', epoch_duration)
            # validation
            if epochs - epoch < 5:
                valid_acc, valid_obj = infer(valid_queue, model, criterion)
                logging.info('Valid_acc %f', valid_acc)
        utils.save(model, os.path.join(args.save, 'weights.pt'))
        print('------Dropping %d paths------' % num_to_drop[sp])
        # Save switches info for s-c refinement.
        if sp == len(num_to_keep) - 1:
            switches_normal_2 = copy.deepcopy(switches_normal)
            switches_reduce_2 = copy.deepcopy(switches_reduce)
        # drop operations with low architecture weights
        arch_param = model.module.arch_parameters()
        normal_prob = F.softmax(arch_param[0], dim=sm_dim).data.cpu().numpy()
        for i in range(14):
            idxs = []
            for j in range(len(PRIMITIVES)):
                if switches_normal[i][j]:
                    idxs.append(j)
            if sp == len(num_to_keep) - 1:
                # for the last stage, drop all Zero operations
                drop = get_min_k_no_zero(normal_prob[i, :], idxs,
                                         num_to_drop[sp])
            else:
                drop = get_min_k(normal_prob[i, :], num_to_drop[sp])
            for idx in drop:
                switches_normal[i][idxs[idx]] = False
        reduce_prob = F.softmax(arch_param[1], dim=-1).data.cpu().numpy()
        for i in range(14):
            idxs = []
            for j in range(len(PRIMITIVES)):
                if switches_reduce[i][j]:
                    idxs.append(j)
            if sp == len(num_to_keep) - 1:
                drop = get_min_k_no_zero(reduce_prob[i, :], idxs,
                                         num_to_drop[sp])
            else:
                drop = get_min_k(reduce_prob[i, :], num_to_drop[sp])
            for idx in drop:
                switches_reduce[i][idxs[idx]] = False
        logging.info('switches_normal = %s', switches_normal)
        logging_switches(switches_normal)
        logging.info('switches_reduce = %s', switches_reduce)
        logging_switches(switches_reduce)

        if sp == len(num_to_keep) - 1:
            arch_param = model.module.arch_parameters()
            normal_prob = F.softmax(arch_param[0],
                                    dim=sm_dim).data.cpu().numpy()
            reduce_prob = F.softmax(arch_param[1],
                                    dim=sm_dim).data.cpu().numpy()
            normal_final = [0 for idx in range(14)]
            reduce_final = [0 for idx in range(14)]
            # remove all Zero operations
            for i in range(14):
                if switches_normal_2[i][0] == True:
                    normal_prob[i][0] = 0
                normal_final[i] = max(normal_prob[i])
                if switches_reduce_2[i][0] == True:
                    reduce_prob[i][0] = 0
                reduce_final[i] = max(reduce_prob[i])
            # Generate Architecture, similar to DARTS
            keep_normal = [0, 1]
            keep_reduce = [0, 1]
            n = 3
            start = 2
            for i in range(3):
                end = start + n
                tbsn = normal_final[start:end]
                tbsr = reduce_final[start:end]
                edge_n = sorted(range(n), key=lambda x: tbsn[x])
                keep_normal.append(edge_n[-1] + start)
                keep_normal.append(edge_n[-2] + start)
                edge_r = sorted(range(n), key=lambda x: tbsr[x])
                keep_reduce.append(edge_r[-1] + start)
                keep_reduce.append(edge_r[-2] + start)
                start = end
                n = n + 1
            # set switches according the ranking of arch parameters
            for i in range(14):
                if not i in keep_normal:
                    for j in range(len(PRIMITIVES)):
                        switches_normal[i][j] = False
                if not i in keep_reduce:
                    for j in range(len(PRIMITIVES)):
                        switches_reduce[i][j] = False
            # translate switches into genotype
            genotype = parse_network(switches_normal, switches_reduce)
            logging.info(genotype)
            ## restrict skipconnect (normal cell only)
            logging.info('Restricting skipconnect...')
            # generating genotypes with different numbers of skip-connect operations
            for sks in range(0, 9):
                max_sk = 8 - sks
                num_sk = check_sk_number(switches_normal)
                if not num_sk > max_sk:
                    continue
                while num_sk > max_sk:
                    normal_prob = delete_min_sk_prob(switches_normal,
                                                     switches_normal_2,
                                                     normal_prob)
                    switches_normal = keep_1_on(switches_normal_2, normal_prob)
                    switches_normal = keep_2_branches(switches_normal,
                                                      normal_prob)
                    num_sk = check_sk_number(switches_normal)
                logging.info('Number of skip-connect: %d', max_sk)
                genotype = parse_network(switches_normal, switches_reduce)
                logging.info(genotype)
Beispiel #48
0
def main(args: Namespace):
    print('Loading data')
    dataset = get_data(args.data_path)
    num_mols, num_tasks = len(dataset), dataset.num_tasks()
    args.num_mols, args.num_tasks = num_mols, num_tasks
    data = convert_moleculedataset_to_moleculefactordataset(dataset)

    print(f'Number of molecules = {num_mols:,}')
    print(f'Number of tasks = {num_tasks:,}')
    print(f'Number of real tasks = {args.num_real_tasks:,}')
    print(f'Number of known molecule-task pairs = {len(data):,}')

    print('Splitting data')
    train_data, val_data, test_data = split_data(data, args)

    if args.dataset_type == 'regression':
        print('Scaling data')
        targets = train_data.targets()
        scaler = StandardScaler().fit(targets)
        scaled_targets = scaler.transform(targets).tolist()
        train_data.set_targets(scaled_targets)
    else:
        scaler = None

    print(
        f'Total size = {len(data):,} | '
        f'train size = {len(train_data):,} | val size = {len(val_data):,} | test size = {len(test_data):,}'
    )

    if args.checkpoint_path is not None:
        print('Loading saved model')
        model, loaded_args = load(args.checkpoint_path)
        assert args.num_mols == loaded_args.num_mols and args.num_tasks == loaded_args.num_tasks
        args.embedding_size, args.hidden_size, args.dropout, args.activation, args.dataset_type = \
            loaded_args.embedding_size, loaded_args.hidden_size, loaded_args.dropout, loaded_args.activation, loaded_args.dataset_type
    else:
        print('Building model')
        model = MatrixFactorizer(
            args,
            num_mols=num_mols,
            num_tasks=num_tasks,
            embedding_size=args.embedding_size,
            hidden_size=args.hidden_size,
            dropout=args.dropout,
            activation=args.activation,
            classification=(args.dataset_type == 'classification'))

    print(model)
    print(f'Number of parameters = {param_count(model):,}')

    loss_func = get_loss_func(args)
    metric_func = get_metric_func(metric=args.metric)
    optimizer = Adam(model.parameters(), lr=args.lr)

    if args.cuda:
        print('Moving model to cuda')
        model = model.cuda()

    print('Training')
    for epoch in trange(args.epochs):
        print(f'Epoch {epoch}')
        train(model=model,
              data=train_data,
              loss_func=loss_func,
              optimizer=optimizer,
              batch_size=args.batch_size,
              random_mol_embeddings=args.random_mol_embeddings)
        val_scores = evaluate(model=model,
                              data=val_data,
                              num_tasks=args.num_real_tasks,
                              metric_func=metric_func,
                              batch_size=args.batch_size,
                              scaler=scaler,
                              random_mol_embeddings=args.random_mol_embeddings)
        print(val_scores)
        avg_val_score = np.mean(val_scores)
        print(f'Validation {args.metric} = {avg_val_score:.6f}')

    test_scores = evaluate(model=model,
                           data=test_data,
                           num_tasks=args.num_real_tasks,
                           metric_func=metric_func,
                           batch_size=args.batch_size,
                           scaler=scaler,
                           random_mol_embeddings=args.random_mol_embeddings)
    print(test_scores)
    avg_test_score = np.mean(test_scores)
    print(f'Test {args.metric} = {avg_test_score:.6f}')

    if args.save_path is not None:
        print('Saving model')
        save(model, args, args.save_path)

    if args.filled_matrix_path is not None:
        print('Filling matrix of data')
        fill_matrix(model, args, data)
Beispiel #49
0
def main():
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)
    writerTf = SummaryWriter('writerTfX')
    np.random.seed(args.seed)
    gpus = [int(i) for i in args.gpu.split(',')]
    if len(gpus) == 1:
        torch.cuda.set_device(int(args.gpu))
    cudnn.benchmark = True
    torch.manual_seed(args.seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(args.seed)
    logging.info('gpu device = %s' % args.gpu)
    logging.info("args = %s", args)

    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    model = Network(args.init_channels, CIFAR_CLASSES, args.layers, criterion)
    model = model.cuda()
    if len(gpus) > 1:
        print("True")
        model = nn.parallel.DataParallel(model,
                                         device_ids=gpus,
                                         output_device=gpus[0])
        model = model.module

    arch_params = list(map(id, model.arch_parameters()))
    weight_params = filter(lambda p: id(p) not in arch_params,
                           model.parameters())

    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    optimizer = torch.optim.SGD(
        # model.parameters(),
        weight_params,
        args.learning_rate,
        momentum=args.momentum,
        weight_decay=args.weight_decay)

    train_transform, valid_transform = utils._data_transforms_cifar10(args)
    train_data = dset.CIFAR10(root=args.data,
                              train=True,
                              download=False,
                              transform=train_transform)

    num_train = len(train_data)
    indices = list(range(num_train))
    split = int(np.floor(args.train_portion * num_train))

    train_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
        pin_memory=True,
        num_workers=2)

    valid_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(
            indices[split:num_train]),
        pin_memory=True,
        num_workers=2)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs), eta_min=args.learning_rate_min)

    architect = Architect(model, criterion, args)

    temperature = 5.0
    exp_anneal_rate = np.exp(-0.045)
    for epoch in range(args.epochs):
        temperature = temperature * exp_anneal_rate  # update temperature
        scheduler.step()
        lr = scheduler.get_lr()[0]
        logging.info('epoch %d lr %e', epoch, lr)

        genotype = model.genotype()
        logging.info('genotype = %s', genotype)

        # print(F.softmax(model.alphas_normal, dim=-1))
        # print(F.softmax(model.alphas_reduce, dim=-1))

        # training
        train_acc, train_obj = train(train_queue, valid_queue, model,
                                     architect, criterion, optimizer, lr,
                                     temperature)
        logging.info('train_acc %f', train_acc)

        writerTf.add_scalar('train_acc', train_acc, epoch)
        writerTf.add_scalar('train_obj', train_obj, epoch)

        # validation
        with torch.no_grad():
            valid_acc, valid_obj = infer(valid_queue, model, criterion)
            writerTf.add_scalar('valid_acc', valid_acc, epoch)
            writerTf.add_scalar('valid_obj', valid_obj, epoch)

        writerTf.close()
        logging.info('valid_acc %f', valid_acc)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
Beispiel #50
0
def main():
    start_time = time.time()
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)

    torch.cuda.set_device(args.gpu)
    reproducibility(args.seed)
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)

    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    model = Network(args.model_name,
                    CIFAR_CLASSES,
                    sub_policies,
                    args.use_cuda,
                    args.use_parallel,
                    temperature=args.temperature,
                    criterion=criterion)
    # model = model.cuda()
    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    optimizer = torch.optim.SGD(model.parameters(),
                                args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    # train_transform, valid_transform = utils._data_transforms_cifar10(args)
    # train_data = dset.CIFAR10(root=args.data, train=True, download=True, transform=train_transform)
    # train_data = AugmCIFAR10(
    #     root=args.data, train=True, download=True,
    #     transform=train_transform, ops_names=sub_policies, search=True, magnitudes=model.magnitudes)
    # valid_data = AugmCIFAR10(
    #     root=args.data, train=True, download=True,
    #     transform=train_transform, ops_names=sub_policies, search=False, magnitudes=model.magnitudes)

    # num_train = len(train_data)
    # indices = list(range(num_train))
    # split = int(np.floor(args.train_portion * num_train))

    # train_queue = torch.utils.data.DataLoader(
    #     train_data, batch_size=args.batch_size,
    #     sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
    #     pin_memory=True, num_workers=args.num_workers)

    # valid_queue = torch.utils.data.DataLoader(
    #     valid_data, batch_size=args.batch_size,
    #     sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[split:num_train]),
    #     pin_memory=True, num_workers=args.num_workers)
    train_queue, valid_queue = get_dataloaders(args.dataset,
                                               args.batch_size,
                                               args.num_workers,
                                               args.dataroot,
                                               sub_policies,
                                               model.magnitudes,
                                               args.cutout,
                                               args.cutout_length,
                                               split=args.train_portion,
                                               split_idx=0,
                                               target_lb=-1)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs), eta_min=args.learning_rate_min)

    architect = Architect(model, args)

    for epoch in range(args.epochs):
        scheduler.step()
        lr = scheduler.get_lr()[0]
        logging.info('epoch %d lr %e', epoch, lr)

        genotype = model.genotype()
        # logging.info('genotype = %s', genotype)
        print_genotype(genotype)
        # logging.info('%s' % str(torch.nn.functional.softmax(model.ops_weights, dim=-1)))
        probs = model.ops_weights
        # logging.info('%s' % str(probs / probs.sum(-1, keepdim=True)))
        logging.info('%s' % str(torch.nn.functional.softmax(probs, dim=-1)))
        logging.info('%s' % str(model.probabilities.clamp(0, 1)))
        logging.info('%s' % str(model.magnitudes.clamp(0, 1)))

        # training
        train_acc, train_obj = train(train_queue, valid_queue, model,
                                     architect, criterion, optimizer, lr)
        logging.info('train_acc %f', train_acc)

        # validation
        valid_acc, valid_obj = infer(valid_queue, model, criterion)
        logging.info('valid_acc %f', valid_acc)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
    end_time = time.time()
    elapsed = end_time - start_time
    logging.info('elapsed time: %.3f Hours' % (elapsed / 3600.))
Beispiel #51
0
def main():
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)

    random.seed(args.seed)
    np.random.seed(
        args.data_seed)  # cutout and load_corrupted_data use np.random
    torch.cuda.set_device(args.gpu)
    cudnn.benchmark = False
    torch.manual_seed(args.seed)
    cudnn.enabled = True
    cudnn.deterministic = True
    torch.cuda.manual_seed(args.seed)
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)

    if args.arch == 'resnet':
        model = ResNet18(CIFAR_CLASSES).cuda()
        args.auxiliary = False
    elif args.arch == 'resnet50':
        model = ResNet50(CIFAR_CLASSES).cuda()
        args.auxiliary = False
    elif args.arch == 'resnet34':
        model = ResNet34(CIFAR_CLASSES).cuda()
        args.auxiliary = False
    else:
        genotype = eval("genotypes.%s" % args.arch)
        model = Network(args.init_channels, CIFAR_CLASSES, args.layers,
                        args.auxiliary, genotype)
        model = model.cuda()

    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    optimizer = torch.optim.SGD(model.parameters(),
                                args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    train_transform, test_transform = utils._data_transforms_cifar10(args)

    # Load dataset
    if args.dataset == 'cifar10':
        noisy_train_data = CIFAR10(root=args.data,
                                   train=True,
                                   gold=False,
                                   gold_fraction=0.0,
                                   corruption_prob=args.corruption_prob,
                                   corruption_type=args.corruption_type,
                                   transform=train_transform,
                                   download=True,
                                   seed=args.data_seed)
        gold_train_data = CIFAR10(root=args.data,
                                  train=True,
                                  gold=True,
                                  gold_fraction=1.0,
                                  corruption_prob=args.corruption_prob,
                                  corruption_type=args.corruption_type,
                                  transform=train_transform,
                                  download=True,
                                  seed=args.data_seed)
        test_data = dset.CIFAR10(root=args.data,
                                 train=False,
                                 download=True,
                                 transform=test_transform)
    elif args.dataset == 'cifar100':
        noisy_train_data = CIFAR100(root=args.data,
                                    train=True,
                                    gold=False,
                                    gold_fraction=0.0,
                                    corruption_prob=args.corruption_prob,
                                    corruption_type=args.corruption_type,
                                    transform=train_transform,
                                    download=True,
                                    seed=args.data_seed)
        gold_train_data = CIFAR100(root=args.data,
                                   train=True,
                                   gold=True,
                                   gold_fraction=1.0,
                                   corruption_prob=args.corruption_prob,
                                   corruption_type=args.corruption_type,
                                   transform=train_transform,
                                   download=True,
                                   seed=args.data_seed)
        test_data = dset.CIFAR100(root=args.data,
                                  train=False,
                                  download=True,
                                  transform=test_transform)

    num_train = len(gold_train_data)
    indices = list(range(num_train))
    split = int(np.floor(args.train_portion * num_train))

    if args.gold_fraction == 1.0:
        train_data = gold_train_data
    else:
        train_data = noisy_train_data
    train_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
        pin_memory=True,
        num_workers=0)

    if args.clean_valid:
        valid_data = gold_train_data
    else:
        valid_data = noisy_train_data

    valid_queue = torch.utils.data.DataLoader(
        valid_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[split:]),
        pin_memory=True,
        num_workers=0)

    test_queue = torch.utils.data.DataLoader(test_data,
                                             batch_size=args.batch_size,
                                             shuffle=False,
                                             pin_memory=True,
                                             num_workers=2)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs))

    if args.loss_func == 'cce':
        criterion = nn.CrossEntropyLoss().cuda()
    elif args.loss_func == 'rll':
        criterion = utils.RobustLogLoss(alpha=args.alpha).cuda()
    elif args.loss_func == 'forward_gold':
        corruption_matrix = train_data.corruption_matrix
        criterion = utils.ForwardGoldLoss(corruption_matrix=corruption_matrix)
    else:
        assert False, "Invalid loss function '{}' given. Must be in {'cce', 'rll'}".format(
            args.loss_func)

    for epoch in range(args.epochs):
        scheduler.step()
        logging.info('epoch %d lr %e', epoch, scheduler.get_lr()[0])
        model.drop_path_prob = args.drop_path_prob * epoch / args.epochs

        train_acc, train_obj = train(train_queue, model, criterion, optimizer)
        logging.info('train_acc %f', train_acc)

        valid_acc, valid_obj = infer_valid(valid_queue, model, criterion)
        logging.info('valid_acc %f', valid_acc)

        test_acc, test_obj = infer(test_queue, model, criterion)
        logging.info('test_acc %f', test_acc)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
def dqn_learning_keras_memoryReplay_v2(env, model, folder_path, EPISODE_MAX,
                                       MAX_STEPS, GAMMA, MEMORY_SIZE,
                                       BATCH_SIZE, EXPLORATION_MAX,
                                       EXPLORATION_MIN, EXPLORATION_DECAY,
                                       observation_space, action_space,
                                       step_size):
    '''
    function which is a neural net using Keras with memory replay
    '''

    # Initializations:
    exploration_rate = EXPLORATION_MAX
    # Experience replay:
    memory = deque(maxlen=MEMORY_SIZE)

    # Create list for plot and saving
    list_memory_history = []
    list_total_reward = []
    list_loss = []
    episode_max = EPISODE_MAX
    list_done = []

    done = False

    for i in range(episode_max):
        # time_start_epoch = time.time()
        total_reward = 0
        j = 0
        state = env.reset()
        rospy.sleep(4.0)
        # Becarefull with the state (np and list)
        np_state = (np.array(state, dtype=np.float32), )
        np_state = np.reshape(np_state, [1, observation_space])
        # np_state = np.identity(7)[np_state:np_state+1]
        # print(np_state)
        done = False
        list_memory = []
        print("*********************************************")
        print("Episode: ", i)
        print("*********************************************")
        while j < MAX_STEPS and not done:  # step inside an episode
            # time_start_step = time.time()

            # print("[INFO]: episode: ", i, ", step: ", j)

            # BECAREFUL with the action!
            action = choose_action(model, np_state, action_space,
                                   exploration_rate)
            disc_action = discrete_action(action, step_size)
            # print(action)
            # time_start_action = time.time()
            # print("################INSIDE ENV################")
            new_state, reward, done, _ = env.step(disc_action)
            # print("################INSIDE ENV################")
            # print("[ INFO] Time for the action ", j, ": ", time.time()-time_start_action)
            # time_start_print = time.time()
            # print("*********************************************")
            # print("Observation: ", new_state)
            # print("State: ", state)
            # print("Reward: ", reward)
            # print("Total rewards: ", total_reward)
            # print("Done: ", done)
            # print("Episode: ", i)
            # print("Step: ", j)
            # print("*********************************************")
            # print("[ INFO] Time for the print info ", j, ": ", time.time()-time_start_print)
            np_new_state = (np.array(new_state, dtype=np.float32), )
            np_new_state = np.reshape(np_new_state, [1, observation_space])
            # np_new_state = np.identity(7)[np_new_state:np_new_state+1]

            # Memory replay
            # memory = save_forReplay(memory, state, action, reward, new_state, done)
            # time_start_save_for_replay = time.time()
            save_forReplay(memory, np_state, action, reward, np_new_state,
                           done)
            # print("[ INFO] Time for the saving of experience replay ", j, ": ", time.time()-time_start_save_for_replay)
            # time_start_experience_replay = time.time()

            # print("[ INFO] Time for the experience replay ", j, ": ", time.time()-time_start_experience_replay)

            list_memory.append([np_state, action, reward, np_new_state, done])

            # print("list_loss: ", list_loss)
            np_state = np_new_state
            state = new_state
            total_reward += reward

            # print("[ INFO] Time for the step ", j, ": ", time.time()-time_start_step)
            j += 1
        model, exploration_rate_new, loss = experience_replay_v2(
            model, memory, BATCH_SIZE, exploration_rate, EXPLORATION_DECAY,
            EXPLORATION_MIN, GAMMA)
        exploration_rate = exploration_rate_new
        # Save the lost function into a list
        if len(memory) > BATCH_SIZE:
            list_loss.append(loss[0])
        list_done.append(done)
        # Save the trajectory and reward
        list_memory_history.append(list_memory)
        list_total_reward.append(total_reward)
        # print(list)
        # print("*********************************************")
        # print("*********************************************")
        # print("[ INFO] Time for the epoch ", i, ": ", time.time()-time_start_epoch)
        # print("*********************************************")
        # print("*********************************************")

        if i % 500 == 0:
            # Save the model
            print("Saving...")

            # model.save('/home/roboticlab14/catkin_ws/src/envirenement_reinforcement_learning/environment_package/src/saves/model/try_1.h5')
            path = folder_path + 'model/'
            name = 'model_'
            total_model_path = path + name + str(i) + '.h5'
            # model.save('/media/roboticlab14/DocumentsToShare/Reinforcement_learning/Datas/learn_to_go_position/model/try_1.h5')
            model.save(total_model_path)
            # Save datas
            print(
                "Saving list_total_reward: ",
                utils.save(list_total_reward,
                           i,
                           arg_path=folder_path + "reward/",
                           arg_name="list_total_reward_"))
            print(
                "Saving list_memory_history: ",
                utils.save(list_memory_history,
                           i,
                           arg_path=folder_path + "trajectory/",
                           arg_name="list_memory_history_"))
            print(
                "Saving list_done: ",
                utils.save(list_done,
                           i,
                           arg_path=folder_path + "done/",
                           arg_name="list_done_"))
            print(
                "Saving list_loss: ",
                utils.save(list_loss,
                           i,
                           arg_path=folder_path + "losses/",
                           arg_name="list_loss_"))
            # Save the memory!
            print(
                "Saving memory: ",
                utils.save(memory,
                           i,
                           arg_path=folder_path + "memory/",
                           arg_name="memory_"))
Beispiel #53
0
    def run(self, restore=False):
        self.black = False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.VIDEORESIZE:
                pygame.display.set_mode(event.size, pygame.RESIZABLE)
                break
        g.init()
        if not self.journal:
            utils.load()
        load_save.retrieve()
        x = g.sx(26)
        y = g.sy(11.2)
        if not self.sugar:
            buttons.Button("new", (x, y))
        x, y = g.cxy2
        dx = g.sy(4)
        self.back_button = buttons.Button("back", (x, y))
        x += dx
        buttons.Button("plus", (x, y))
        x += dx
        buttons.Button("times", (x, y))
        x += dx
        buttons.Button("equals", (x, y))
        self.ops = ['back', 'plus', 'times', 'equals']
        if not self.sugar:
            self.slider = slider.Slider(g.sx(22.4), g.sy(20.5), 10,
                                        utils.GREEN)
        self.mouse_auto = True
        self.anim_ms = None

        self.level1()  # initial animation
        self.scored = False
        if restore:
            g.score = self.save_score
        ctrl = False
        pygame.key.set_repeat(600, 120)
        key_ms = pygame.time.get_ticks()
        going = True
        if self.canvas is not None:
            self.canvas.grab_focus()
        while going:
            if self.journal:
                # Pump GTK messages.
                while Gtk.events_pending():
                    Gtk.main_iteration()

            # Pump PyGame messages.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    if not self.journal:
                        utils.save()
                    going = False
                elif event.type == pygame.MOUSEMOTION:
                    g.pos = event.pos
                    g.redraw = True
                    if self.canvas is not None:
                        self.canvas.grab_focus()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    g.redraw = True
                    self.anim_end()
                    if event.button == 1:
                        bu = buttons.check()
                        if bu == '':
                            if not self.check_numbers():
                                if not self.sugar:
                                    if self.slider.mouse():
                                        self.level1()
                        else:
                            self.do_button(bu)  # eg do_button('plus')
                    self.flush_queue()
                elif event.type == pygame.KEYDOWN:
                    self.anim_end()
                    # throttle keyboard repeat
                    if pygame.time.get_ticks() - key_ms > 110:
                        key_ms = pygame.time.get_ticks()
                        if ctrl:
                            if event.key == pygame.K_q:
                                if not self.journal:
                                    utils.save()
                                going = False
                                break
                            else:
                                ctrl = False
                        if event.key in (pygame.K_LCTRL, pygame.K_RCTRL):
                            ctrl = True
                            break
                        self.do_key(event.key)
                        g.redraw = True
                        self.flush_queue()
                elif event.type == pygame.KEYUP:
                    ctrl = False
            if not going:
                break
            self.animation()
            if g.redraw:
                self.display()
                if g.version_display:
                    utils.version_display()
                if not self.black:
                    g.screen.blit(g.pointer, g.pos)
                pygame.display.flip()
                g.redraw = False
            g.clock.tick(40)
try:
    ingredient_list = utils.load(ing_file)
    print("loaded ingredient list from disk...")
except Exception as e:
    print('started 1')
    idh.getIngridient(data1, ingredient_list)
    print('ended 1')

    print('started 2')
    idh.getIngridient(data2, ingredient_list)
    print('ended 1')

    print('started 3')
    idh.getIngridient(data3, ingredient_list)
    print('ended 3')

    utils.save(ingredient_list, ing_file)
    print('saved ingredient list to disk...')

data = {}
data.update(data1)
data.update(data2)
data.update(data3)
utils.save(data, filename=path.join(datadir, 'datadict.pkl'))

idh.wrd2ingrdtdict2tsv(ingredient_list, data, path.join(
    datadir,
    'bigdict.tsv'))  # Unless you wanna freeze your computer, DON'T OPEN IT! XD
idh.print_dict_stats(ingredient_list, data,
                     path.join(datadir, 'bigdict_stats.tsv'))
Beispiel #55
0
def main():
  parser = argparse.ArgumentParser("Common Argument Parser")
  parser.add_argument('--data', type=str, default='../data', help='location of the data corpus')
  parser.add_argument('--dataset', type=str, default='cifar10', help='which dataset:\
                      cifar10, mnist, emnist, fashion, svhn, stl10, devanagari')
  parser.add_argument('--batch_size', type=int, default=64, help='batch size')
  parser.add_argument('--learning_rate', type=float, default=0.025, help='init learning rate')
  parser.add_argument('--learning_rate_min', type=float, default=1e-8, help='min learning rate')
  parser.add_argument('--lr_power_annealing_exponent_order', type=float, default=2,
                      help='Cosine Power Annealing Schedule Base, larger numbers make '
                           'the exponential more dominant, smaller make cosine more dominant, '
                           '1 returns to standard cosine annealing.')
  parser.add_argument('--momentum', type=float, default=0.9, help='momentum')
  parser.add_argument('--weight_decay', '--wd', dest='weight_decay', type=float, default=3e-4, help='weight decay')
  parser.add_argument('--partial', default=1/8, type=float, help='partially adaptive parameter p in Padam')
  parser.add_argument('--report_freq', type=float, default=50, help='report frequency')
  parser.add_argument('--gpu', type=int, default=0, help='gpu device id')
  parser.add_argument('--epochs', type=int, default=2000, help='num of training epochs')
  parser.add_argument('--start_epoch', default=1, type=int, metavar='N',
                      help='manual epoch number (useful for restarts)')
  parser.add_argument('--warmup_epochs', type=int, default=5, help='num of warmup training epochs')
  parser.add_argument('--warm_restarts', type=int, default=20, help='warm restarts of cosine annealing')
  parser.add_argument('--init_channels', type=int, default=36, help='num of init channels')
  parser.add_argument('--mid_channels', type=int, default=32, help='C_mid channels in choke SharpSepConv')
  parser.add_argument('--layers', type=int, default=20, help='total number of layers')
  parser.add_argument('--model_path', type=str, default='saved_models', help='path to save the model')
  parser.add_argument('--auxiliary', action='store_true', default=False, help='use auxiliary tower')
  parser.add_argument('--mixed_auxiliary', action='store_true', default=False, help='Learn weights for auxiliary networks during training. Overrides auxiliary flag')
  parser.add_argument('--auxiliary_weight', type=float, default=0.4, help='weight for auxiliary loss')
  parser.add_argument('--cutout', action='store_true', default=False, help='use cutout')
  parser.add_argument('--cutout_length', type=int, default=16, help='cutout length')
  parser.add_argument('--autoaugment', action='store_true', default=False, help='use cifar10 autoaugment https://arxiv.org/abs/1805.09501')
  parser.add_argument('--random_eraser', action='store_true', default=False, help='use random eraser')
  parser.add_argument('--drop_path_prob', type=float, default=0.2, help='drop path probability')
  parser.add_argument('--save', type=str, default='EXP', help='experiment name')
  parser.add_argument('--seed', type=int, default=0, help='random seed')
  parser.add_argument('--arch', type=str, default='DARTS', help='which architecture to use')
  parser.add_argument('--ops', type=str, default='OPS', help='which operations to use, options are OPS and DARTS_OPS')
  parser.add_argument('--primitives', type=str, default='PRIMITIVES',
                      help='which primitive layers to use inside a cell search space,'
                           ' options are PRIMITIVES, SHARPER_PRIMITIVES, and DARTS_PRIMITIVES')
  parser.add_argument('--optimizer', type=str, default='sgd', help='which optimizer to use, options are padam and sgd')
  parser.add_argument('--load', type=str, default='',  metavar='PATH', help='load weights at specified location')
  parser.add_argument('--grad_clip', type=float, default=5, help='gradient clipping')
  parser.add_argument('--flops', action='store_true', default=False, help='count flops and exit, aka floating point operations.')
  parser.add_argument('-e', '--evaluate', dest='evaluate', type=str, metavar='PATH', default='',
                      help='evaluate model at specified path on training, test, and validation datasets')
  parser.add_argument('--multi_channel', action='store_true', default=False, help='perform multi channel search, a completely separate search space')
  parser.add_argument('--load_args', type=str, default='',  metavar='PATH',
                      help='load command line args from a json file, this will override '
                           'all currently set args except for --evaluate, and arguments '
                           'that did not exist when the json file was originally saved out.')
  parser.add_argument('--layers_of_cells', type=int, default=8, help='total number of cells in the whole network, default is 8 cells')
  parser.add_argument('--layers_in_cells', type=int, default=4,
                      help='Total number of nodes in each cell, aka number of steps,'
                           ' default is 4 nodes, which implies 8 ops')
  parser.add_argument('--weighting_algorithm', type=str, default='scalar',
                    help='which operations to use, options are '
                         '"max_w" (1. - max_w + w) * op, and scalar (w * op)')
  # TODO(ahundt) remove final path and switch back to genotype
  parser.add_argument('--load_genotype', type=str, default=None, help='Name of genotype to be used')
  parser.add_argument('--simple_path', default=True, action='store_false', help='Final model is a simple path (MultiChannelNetworkModel)')
  args = parser.parse_args()

  args = utils.initialize_files_and_args(args)

  logger = utils.logging_setup(args.log_file_path)

  if not torch.cuda.is_available():
    logger.info('no gpu device available')
    sys.exit(1)

  np.random.seed(args.seed)
  torch.cuda.set_device(args.gpu)
  cudnn.benchmark = True
  torch.manual_seed(args.seed)
  cudnn.enabled=True
  torch.cuda.manual_seed(args.seed)
  logger.info('gpu device = %d' % args.gpu)
  logger.info("args = %s", args)

  DATASET_CLASSES = dataset.class_dict[args.dataset]
  DATASET_CHANNELS = dataset.inp_channel_dict[args.dataset]
  DATASET_MEAN = dataset.mean_dict[args.dataset]
  DATASET_STD = dataset.std_dict[args.dataset]
  logger.info('output channels: ' + str(DATASET_CLASSES))

  # # load the correct ops dictionary
  op_dict_to_load = "operations.%s" % args.ops
  logger.info('loading op dict: ' + str(op_dict_to_load))
  op_dict = eval(op_dict_to_load)

  # load the correct primitives list
  primitives_to_load = "genotypes.%s" % args.primitives
  logger.info('loading primitives:' + primitives_to_load)
  primitives = eval(primitives_to_load)
  logger.info('primitives: ' + str(primitives))

  genotype = eval("genotypes.%s" % args.arch)
  # create the neural network

  criterion = nn.CrossEntropyLoss()
  criterion = criterion.cuda()
  if args.multi_channel:
    final_path = None
    if args.load_genotype is not None:
      genotype = getattr(genotypes, args.load_genotype)
      print(genotype)
      if type(genotype[0]) is str:
        logger.info('Path :%s', genotype)
    # TODO(ahundt) remove final path and switch back to genotype
    cnn_model = MultiChannelNetwork(
      args.init_channels, DATASET_CLASSES, layers=args.layers_of_cells, criterion=criterion, steps=args.layers_in_cells,
      weighting_algorithm=args.weighting_algorithm, genotype=genotype)
    flops_shape = [1, 3, 32, 32]
  elif args.dataset == 'imagenet':
      cnn_model = NetworkImageNet(args.init_channels, DATASET_CLASSES, args.layers, args.auxiliary, genotype, op_dict=op_dict, C_mid=args.mid_channels)
      flops_shape = [1, 3, 224, 224]
  else:
      cnn_model = NetworkCIFAR(args.init_channels, DATASET_CLASSES, args.layers, args.auxiliary, genotype, op_dict=op_dict, C_mid=args.mid_channels)
      flops_shape = [1, 3, 32, 32]
  cnn_model = cnn_model.cuda()

  logger.info("param size = %fMB", utils.count_parameters_in_MB(cnn_model))
  if args.flops:
    logger.info('flops_shape = ' + str(flops_shape))
    logger.info("flops = " + utils.count_model_flops(cnn_model, data_shape=flops_shape))
    return

  optimizer = torch.optim.SGD(
      cnn_model.parameters(),
      args.learning_rate,
      momentum=args.momentum,
      weight_decay=args.weight_decay
      )

  # Get preprocessing functions (i.e. transforms) to apply on data
  train_transform, valid_transform = utils.get_data_transforms(args)
  if args.evaluate:
    # evaluate the train dataset without augmentation
    train_transform = valid_transform

  # Get the training queue, use full training and test set
  train_queue, valid_queue = dataset.get_training_queues(
    args.dataset, train_transform, valid_transform, args.data, args.batch_size, train_proportion=1.0, search_architecture=False)

  test_queue = None
  if args.dataset == 'cifar10':
    # evaluate best model weights on cifar 10.1
    # https://github.com/modestyachts/CIFAR-10.1
    test_data = cifar10_1.CIFAR10_1(root=args.data, download=True, transform=valid_transform)
    test_queue = torch.utils.data.DataLoader(
      test_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=8)

  if args.evaluate:
    # evaluate the loaded model, print the result, and return
    logger.info("Evaluating inference with weights file: " + args.load)
    eval_stats = evaluate(
      args, cnn_model, criterion, args.load,
      train_queue=train_queue, valid_queue=valid_queue, test_queue=test_queue)
    with open(args.stats_file, 'w') as f:
      arg_dict = vars(args)
      arg_dict.update(eval_stats)
      json.dump(arg_dict, f)
    logger.info("flops = " + utils.count_model_flops(cnn_model))
    logger.info(utils.dict_to_log_string(eval_stats))
    logger.info('\nEvaluation of Loaded Model Complete! Save dir: ' + str(args.save))
    return

  lr_schedule = cosine_power_annealing(
    epochs=args.epochs, max_lr=args.learning_rate, min_lr=args.learning_rate_min,
    warmup_epochs=args.warmup_epochs, exponent_order=args.lr_power_annealing_exponent_order)
  epochs = np.arange(args.epochs) + args.start_epoch
  # scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, float(args.epochs))
  epoch_stats = []

  stats_csv = args.epoch_stats_file
  stats_csv = stats_csv.replace('.json', '.csv')
  with tqdm(epochs, dynamic_ncols=True) as prog_epoch:
    best_valid_acc = 0.0
    best_epoch = 0
    best_stats = {}
    stats = {}
    epoch_stats = []
    weights_file = os.path.join(args.save, 'weights.pt')
    for epoch, learning_rate in zip(prog_epoch, lr_schedule):
      # update the drop_path_prob augmentation
      cnn_model.drop_path_prob = args.drop_path_prob * epoch / args.epochs
      # update the learning rate
      for param_group in optimizer.param_groups:
        param_group['lr'] = learning_rate
      # scheduler.get_lr()[0]

      train_acc, train_obj = train(args, train_queue, cnn_model, criterion, optimizer)

      val_stats = infer(args, valid_queue, cnn_model, criterion)
      stats.update(val_stats)
      stats['train_acc'] = train_acc
      stats['train_loss'] = train_obj
      stats['lr'] = learning_rate
      stats['epoch'] = epoch

      if stats['valid_acc'] > best_valid_acc:
        # new best epoch, save weights
        utils.save(cnn_model, weights_file)
        best_epoch = epoch
        best_stats.update(copy.deepcopy(stats))
        best_valid_acc = stats['valid_acc']
        best_train_loss = train_obj
        best_train_acc = train_acc
      # else:
      #   # not best epoch, load best weights
      #   utils.load(cnn_model, weights_file)
      logger.info('epoch, %d, train_acc, %f, valid_acc, %f, train_loss, %f, valid_loss, %f, lr, %e, best_epoch, %d, best_valid_acc, %f, ' + utils.dict_to_log_string(stats),
                  epoch, train_acc, stats['valid_acc'], train_obj, stats['valid_loss'], learning_rate, best_epoch, best_valid_acc)
      stats['train_acc'] = train_acc
      stats['train_loss'] = train_obj
      epoch_stats += [copy.deepcopy(stats)]
      with open(args.epoch_stats_file, 'w') as f:
        json.dump(epoch_stats, f, cls=utils.NumpyEncoder)
      utils.list_of_dicts_to_csv(stats_csv, epoch_stats)

    # get stats from best epoch including cifar10.1
    eval_stats = evaluate(args, cnn_model, criterion, weights_file, train_queue, valid_queue, test_queue)
    with open(args.stats_file, 'w') as f:
      arg_dict = vars(args)
      arg_dict.update(eval_stats)
      json.dump(arg_dict, f, cls=utils.NumpyEncoder)
    with open(args.epoch_stats_file, 'w') as f:
      json.dump(epoch_stats, f, cls=utils.NumpyEncoder)
    logger.info(utils.dict_to_log_string(eval_stats))
    logger.info('Training of Final Model Complete! Save dir: ' + str(args.save))
Beispiel #56
0
def main():
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)

    np.random.seed(args.seed)
    torch.cuda.set_device(args.gpu)
    cudnn.benchmark = True
    cudnn.enabled = True
    torch.cuda.manual_seed(args.seed)
    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)
    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    model = Network(args.init_channels, CIFAR_CLASSES, args.layers, criterion)
    model = model.cuda()
    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    optimizer = torch.optim.SGD(model.parameters(),
                                args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    train_transform, valid_transform = utils._data_transforms_cifar10(args)
    train_data = dset.CIFAR10(root=args.data,
                              train=True,
                              download=True,
                              transform=train_transform)

    num_train = len(train_data)
    indices = list(range(num_train))
    split = int(np.floor(args.train_portion * num_train))

    train_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(indices[:split]),
        pin_memory=True,
        num_workers=2)

    valid_queue = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size,
        sampler=torch.utils.data.sampler.SubsetRandomSampler(
            indices[split:num_train]),
        pin_memory=True,
        num_workers=2)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs), eta_min=args.learning_rate_min)

    architect = Architect(model, args)

    for epoch in range(args.epochs):
        scheduler.step()
        lr = scheduler.get_lr()[0]
        logging.info('epoch %d lr %e', epoch, lr)

        genotype = model.genotype()
        logging.info('genotype = %s', genotype)

        print(F.softmax(model.alphas_normal, dim=-1))
        print(F.softmax(model.alphas_reduce, dim=-1))

        # training
        train_acc, train_obj = train(train_queue, valid_queue, model,
                                     architect, criterion, optimizer, lr)
        logging.info('train_acc %f', train_acc)

        # validation
        valid_acc, valid_obj = infer(valid_queue, model, criterion)
        logging.info('valid_acc %f', valid_acc)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
Beispiel #57
0
def main():
  if not torch.cuda.is_available():
    logger.info('no gpu device available')
    sys.exit(1)

  np.random.seed(args.seed)
  torch.cuda.set_device(args.gpu)
  cudnn.benchmark = True
  torch.manual_seed(args.seed)
  cudnn.enabled=True
  torch.cuda.manual_seed(args.seed)
  logger.info('gpu device = %d' % args.gpu)
  logger.info("args = %s", args)

  # # load the correct ops dictionary
  op_dict_to_load = "operations.%s" % args.ops
  logger.info('loading op dict: ' + str(op_dict_to_load))
  op_dict = eval(op_dict_to_load)

  # load the correct primitives list
  primitives_to_load = "genotypes.%s" % args.primitives
  logger.info('loading primitives:' + primitives_to_load)
  primitives = eval(primitives_to_load)
  logger.info('primitives: ' + str(primitives))

  genotype = eval("genotypes.%s" % args.arch)
  cnn_model = Network(args.init_channels, CLASSES, args.layers, args.auxiliary, genotype, op_dict=op_dict, C_mid=args.mid_channels)
  if args.parallel:
    cnn_model = nn.DataParallel(cnn_model).cuda()
  else:
    cnn_model = cnn_model.cuda()

  logger.info("param size = %fMB", utils.count_parameters_in_MB(cnn_model))
  if args.flops:
    cnn_model.drop_path_prob = 0.0
    logger.info("flops = " + utils.count_model_flops(cnn_model, data_shape=[1, 3, 224, 224]))
    exit(1)

  criterion = nn.CrossEntropyLoss()
  criterion = criterion.cuda()
  criterion_smooth = CrossEntropyLabelSmooth(CLASSES, args.label_smooth)
  criterion_smooth = criterion_smooth.cuda()

  optimizer = torch.optim.SGD(
    cnn_model.parameters(),
    args.learning_rate,
    momentum=args.momentum,
    weight_decay=args.weight_decay
    )

  traindir = os.path.join(args.data, 'train')
  validdir = os.path.join(args.data, 'val')
  normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  train_data = dset.ImageFolder(
    traindir,
    transforms.Compose([
      transforms.RandomResizedCrop(224),
      transforms.RandomHorizontalFlip(),
      autoaugment.ImageNetPolicy(),
      # transforms.ColorJitter(
      #   brightness=0.4,
      #   contrast=0.4,
      #   saturation=0.4,
      #   hue=0.2),
      transforms.ToTensor(),
      normalize,
    ]))
  valid_data = dset.ImageFolder(
    validdir,
    transforms.Compose([
      transforms.Resize(256),
      transforms.CenterCrop(224),
      transforms.ToTensor(),
      normalize,
    ]))

  train_queue = torch.utils.data.DataLoader(
    train_data, batch_size=args.batch_size, shuffle=True, pin_memory=True, num_workers=8)

  valid_queue = torch.utils.data.DataLoader(
    valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=8)

  scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.decay_period, gamma=args.gamma)

  prog_epoch = tqdm(range(args.epochs), dynamic_ncols=True)
  best_valid_acc = 0.0
  best_epoch = 0
  best_stats = {}
  best_acc_top1 = 0
  weights_file = os.path.join(args.save, 'weights.pt')
  for epoch in prog_epoch:
    scheduler.step()
    cnn_model.drop_path_prob = args.drop_path_prob * epoch / args.epochs

    train_acc, train_obj = train.train(args, train_queue, cnn_model, criterion, optimizer)
    stats = train.infer(args, valid_queue, cnn_model, criterion)

    is_best = False
    if stats['valid_acc'] > best_valid_acc:
      # new best epoch, save weights
      utils.save(cnn_model, weights_file)
      best_epoch = epoch
      best_valid_acc = stats['valid_acc']

      best_stats = stats
      best_stats['lr'] = scheduler.get_lr()[0]
      best_stats['epoch'] = best_epoch
      best_train_loss = train_obj
      best_train_acc = train_acc
      is_best = True

    logger.info('epoch, %d, train_acc, %f, valid_acc, %f, train_loss, %f, valid_loss, %f, lr, %e, best_epoch, %d, best_valid_acc, %f, ' + utils.dict_to_log_string(stats),
                epoch, train_acc, stats['valid_acc'], train_obj, stats['valid_loss'], scheduler.get_lr()[0], best_epoch, best_valid_acc)
    checkpoint = {
          'epoch': epoch,
          'state_dict': cnn_model.state_dict(),
          'best_acc_top1': best_valid_acc,
          'optimizer' : optimizer.state_dict(),
    }
    checkpoint.update(stats)
    utils.save_checkpoint(stats, is_best, args.save)

  best_epoch_str = utils.dict_to_log_string(best_stats, key_prepend='best_')
  logger.info(best_epoch_str)
  logger.info('Training of Final Model Complete! Save dir: ' + str(args.save))
Beispiel #58
0
def main():
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)

    np.random.seed(args.seed)
    cudnn.benchmark = True
    torch.manual_seed(args.seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(args.seed)
    logging.info('gpu device = %s' % args.gpus)
    logging.info("args = %s", args)
    num_gpus = torch.cuda.device_count()
    genotype = eval("genotypes.%s" % args.arch)
    print('---------Genotype---------')
    logging.info(genotype)
    print('--------------------------')
    model = Network(args.init_channels, CIFAR_CLASSES, args.layers,
                    args.auxiliary, genotype)
    if num_gpus > 1:
        model = nn.DataParallel(model)
        model = model.cuda()
    else:
        model = model.cuda()
    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    optimizer = torch.optim.SGD(model.parameters(),
                                args.learning_rate,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    train_transform, valid_transform = utils._data_transforms_cifar10(args)
    if args.set == 'cifar100':
        train_data = dset.CIFAR100(root=args.data,
                                   train=True,
                                   download=True,
                                   transform=train_transform)
        valid_data = dset.CIFAR100(root=args.data,
                                   train=False,
                                   download=True,
                                   transform=valid_transform)
    else:
        train_data = dset.CIFAR10(root=args.data,
                                  train=True,
                                  download=True,
                                  transform=train_transform)
        valid_data = dset.CIFAR10(root=args.data,
                                  train=False,
                                  download=True,
                                  transform=valid_transform)
    #train_data = dset.CIFAR10(root=args.data, train=True, download=True, transform=train_transform)
    #valid_data = dset.CIFAR10(root=args.data, train=False, download=True, transform=valid_transform)

    train_queue = torch.utils.data.DataLoader(train_data,
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              pin_memory=True,
                                              num_workers=args.workers)

    valid_queue = torch.utils.data.DataLoader(valid_data,
                                              batch_size=args.batch_size,
                                              shuffle=False,
                                              pin_memory=True,
                                              num_workers=args.workers)

    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, float(args.epochs))
    best_acc = 0.0
    for epoch in range(args.epochs):
        scheduler.step()
        logging.info('epoch %d lr %e', epoch, scheduler.get_lr()[0])
        if num_gpus > 1:
            model.module.drop_path_prob = args.drop_path_prob * epoch / args.epochs
        else:
            model.drop_path_prob = args.drop_path_prob * epoch / args.epochs

        train_acc, train_obj = train(train_queue, model, criterion, optimizer)
        logging.info('train_acc %f', train_acc)

        valid_acc, valid_obj = infer(valid_queue, model, criterion)
        if valid_acc > best_acc:
            best_acc = valid_acc
        logging.info('valid_acc %f, best_acc %f', valid_acc, best_acc)

        utils.save(model, os.path.join(args.save, 'weights.pt'))
Beispiel #59
0
def main():
    html = utils.fetch_url(url)
    process(html)
    utils.save(json_filename, moves_list)
Beispiel #60
0
def main():
    if not torch.cuda.is_available():
        logging.info('No GPU device available')
        sys.exit(1)
    np.random.seed(args.seed)
    cudnn.benchmark = True
    torch.manual_seed(args.seed)
    cudnn.enabled=True
    torch.cuda.manual_seed(args.seed)
    logging.info("args = %s", args)
    logging.info("unparsed args = %s", unparsed)
    num_gpus = torch.cuda.device_count()
    
    genotype = eval("genotypes.%s" % args.arch)
    print('---------Genotype---------')
    logging.info(genotype)
    print('--------------------------')
    model = Network(args.init_channels, CIFAR_CLASSES, args.layers, args.auxiliary, genotype)
    model = torch.nn.DataParallel(model)
    model = model.cuda()
    logging.info("param size = %fMB", utils.count_parameters_in_MB(model))

    criterion = nn.CrossEntropyLoss()
    criterion = criterion.cuda()
    optimizer = torch.optim.SGD(
        model.parameters(),
        args.learning_rate,
        momentum=args.momentum,
        weight_decay=args.weight_decay
        )

    if args.cifar100:
        train_transform, valid_transform = utils._data_transforms_cifar100(args)
    else:
        train_transform, valid_transform = utils._data_transforms_cifar10(args)
    if args.cifar100:
        train_data = dset.CIFAR100(root=args.tmp_data_dir, train=True, download=True, transform=train_transform)
        valid_data = dset.CIFAR100(root=args.tmp_data_dir, train=False, download=True, transform=valid_transform)
    else:
        train_data = dset.CIFAR10(root=args.tmp_data_dir, train=True, download=True, transform=train_transform)
        valid_data = dset.CIFAR10(root=args.tmp_data_dir, train=False, download=True, transform=valid_transform)

    train_queue = torch.utils.data.DataLoader(
        train_data, batch_size=args.batch_size, shuffle=True, pin_memory=True, num_workers=args.workers)

    valid_queue = torch.utils.data.DataLoader(
        valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=args.workers)
    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, float(args.epochs))
    best_acc = 0.0
    for epoch in range(args.epochs):
        scheduler.step()
        logging.info('Epoch: %d lr %e', epoch, scheduler.get_lr()[0])
        model.module.drop_path_prob = args.drop_path_prob * epoch / args.epochs
        model.drop_path_prob = args.drop_path_prob * epoch / args.epochs
        start_time = time.time()
        train_acc, train_obj = train(train_queue, model, criterion, optimizer)
        logging.info('Train_acc: %f', train_acc)

        valid_acc, valid_obj = infer(valid_queue, model, criterion)
        if valid_acc > best_acc:
            best_acc = valid_acc
        logging.info('Valid_acc: %f', valid_acc)
        end_time = time.time()
        duration = end_time - start_time
        print('Epoch time: %ds.' % duration )
        utils.save(model.module, os.path.join(args.save, 'weights.pt'))