예제 #1
0
    def evaluate(individual, x_train_reduced, y_train_reduced, x_test_reduced,
                 y_test_reduced, x_val_reduced, y_val_reduced, MATRIX):
        model = cifar100vgg(train=0)  # build the model
        model.load(
            'R:\\coco_dataset\\dati_salvati\\cifar100_baseline.h5'
        )  #load the pre trained model, change the path to the saved model
        _ = model.selection_pruning(individual)  #

        score_val, score_test = model.train(x_train_reduced, y_train_reduced,
                                            x_test_reduced, y_test_reduced,
                                            x_val_reduced, y_val_reduced, 3,
                                            'R:\\')

        total_score = 0.6 * score_val + 0.4 * (sum(individual))
        # total_score=5
        # score_val=2
        # score_test=3
        MATRIX.append([individual, score_val, score_test, total_score])
        # gen_matrix.append([mega_list,score,sum(individual),total_score])
        del model  # delete the model and clear the memory
        K.clear_session()
        tf.reset_default_graph()
        return total_score, MATRIX
 def random_pruning(P,num):  # total random pruning 
     matrix=[]
     # model.train(model,x_train_reduced,y_train_reduced,x_test_reduced,y_test_reduced,3)
     for i in range(num):
         print('iteration number:',i)
         model = cifar100vgg(train=0) # build the model
         model.load('R:\mygithub\\'+'cifar100_baseline.h5') #load the pre trained model, change the path to the saved model
         matrix=model.random_pruning(matrix,P)
         score=model.evaluate(x_train_reduced,y_train_reduced,x_test_reduced,y_test_reduced,x_val_reduced,y_val_reduced,3)
         _,score=model.train(model,x_train_reduced,y_train_reduced,x_test_reduced,y_test_reduced,x_val_reduced,y_val_reduced,3)
         matrix[-1].append(score)
         del model # delete the model and clear the memory 
         K.clear_session()
         tf.reset_default_graph()
     path='R:\\matrix_{}'.format(int(P*100)) #path to the saved matrix folder 
     if not os.path.exists(path):
         os.mkdir(path)    
     # np.save(path+'\\'+'matrix_{}_{}'.format(int(P*100),P,time.time()),matrix) # save the matrix containing all the filters and score to file 
     
     filters=[]
     result=[]
     for i in os.listdir(path):
         final_matrix=np.load(os.path.join(path,i),allow_pickle=True)
         for k in range(len(final_matrix)): # ciclo su tutte le prove
             filters.append(final_matrix[k][0:-2])
             result.append(final_matrix[k][-1])
     idx=np.array(result)
     idx=idx[:,-1]
     idx=(-idx).argsort()
     filterS=np.array(filters)[idx]
     resulT=np.array(result)[idx]
     new_path=path.replace('{}'.format(int(P*100)),'')
     if not os.path.exists(new_path):
         os.mkdir(new_path)
     np.save(new_path+'\\'+'result_{}'.format(P),resulT)
     np.save(new_path+'\\'+'filters_{}'.format(P),filterS)
예제 #3
0
        time.sleep(2)
    if debug == 1:
        print('lavoro trovato, leggo files...')
    time.sleep(2)
    pop_list = np.load(os.path.join(operation_path, job_to_do),
                       allow_pickle=True)  # carico la lista di lavoro
    job_to_do = job_to_do.replace('.npy', '')
    var = job_to_do[-3::]
    if debug == 1:
        print('var=', var)
    os.remove(os.path.join(
        operation_path, job_to_do +
        '.npy'))  # ho letto la lista del lavoro da fare, quindi la elimino

    for idx, i in enumerate(pop_list):
        model = cifar100vgg(train=0)

        model.load(
            os.path.join(operation_path, 'cifar100_baseline.h5')
        )  #load the pre trained model, change the path to the saved model

        _ = model.selection_pruning(i)
        model.give_name(
            str(i)
        )  # i è un individuo che è stato castato a lista, tuttavia funziona list(individuo)==individuo
        model.save(
            os.path.join(operation_path, 'job_{}_{}.h5'.format(var, idx)))

        del model  # delete the model and clear the memory
        K.clear_session()
        tf.reset_default_graph()
예제 #4
0
            pop_list)

    for idx, i in enumerate(pop):

        while not os.path.isfile(
                os.path.join(operation_path, 'job_{}_{}.h5'.format(var, idx))):
            time.sleep(2)  # aspetto che il secondo kernel effettui il pruning

            if debug == 1:
                print('attendo svolgimento lavoro {}'.format(idx))

        if debug == 1:
            print('lavoro {} svolto'.format(idx))

        print('individuo numero{} su {}'.format(idx, len(pop)))
        model = cifar100vgg(train=0)  # build the model

        model.load(
            os.path.join(operation_path, 'job_{}_{}.h5'.format(var, idx)))
        ind_name = model.return_name()
        # if debug==1:
        #     if str(list(i)) !=ind_name:
        #         print('ATTENZIONE INDIVIDUO DIVERSO')
        #         time.sleep(100)
        #     elif str(list(i)) ==ind_name:
        #          print('individuo corretto')
        #          time.sleep(5)

        # ind_name=model.return_name()
        # indice=pop.index(ind_name)
        # print('indice=',indice)