Пример #1
0
from resnext import build_resnext
from resnet import build_resnet
from keras.models import Model, load_model
from keras.callbacks import ModelCheckpoint, TensorBoard, LambdaCallback
from keras_contrib.utils.save_load_utils import save_all_weights, load_all_weights
from keras.utils import plot_model
from keras.optimizers import Nadam, Adamax, Adam, SGD, RMSprop
from sklearn import metrics
from sklearn.model_selection import train_test_split
import dpn
import time

os.environ['CUDA_VISIBLE_DEVICES'] = '2,3,4,5'

train_df = pre.load_data('train.json')
images = pre.get_images(train_df)
labels = pre.get_labels(train_df)
del (train_df)

X_train, X_val, y_train, y_val = train_test_split(images,
                                                  labels,
                                                  test_size=0.2,
                                                  random_state=None)

img_gen = pre.images_generator()
print 'Images generator inits completely'

print 'training images:', X_train.shape
print 'validation images:', X_val.shape
print 'training labels:', y_train.shape
print 'validaiton labels:', y_val.shape
Пример #2
0
INPUT_SIZE = 224, 224
WAVE_LENGTH = 4000 # Number of samples in the audio wave
BATCH_SIZE = 128


device = torch.device("cpu")

data_raw_path = "./data/train/"
data_processed_path = "./processed_data/"
img_dir = "./img_data"


# generate images of melspectrograms from the original signals, if not already done
get_data = False
if get_data :
    get_images(data_raw_path, img_dir, input_shape = INPUT_SIZE, wave_length= WAVE_LENGTH)


# fix random state
np.random.seed(RANDOM_STATE)
torch.manual_seed(RANDOM_STATE)

# initialize our transfer network

resnet = resnet18(pretrained = True)
class CnnTransferNet(nn.Module):
    def __init__(self):
        super(CnnTransferNet,self).__init__()
        
        # freeze all layers except fully connected for which output features are changed
        self.resnet =  resnet
Пример #3
0
                time_all += time_cost
                print(f'\tbatch loss = {loss},\ttime = {time_cost} s')

            loss_all, _ = self.get_loss_and_eta(train_pred_proba_all,
                                                train_labels)  # 所有训练集的 loss
            test_pred_proba = self.get_pred_proba(test_imgs)
            f1_all = f1_score(test_labels,
                              self.get_labels(test_pred_proba))  # 测试集的 F1
            print(f'iteration loss = {loss_all},\ttime = {time_all}')
            print(f'iteration F1 = {f1_all}')
            print()

    def save(self, folder_path):
        self.deepid1.save(os.path.join(folder_path, 'deepid1'))
        self.deepid2.save(os.path.join(folder_path, 'deepid2'))
        self.fc_cross.save(os.path.join(folder_path, 'fc_cross'))
        self.fc_softmax.save(os.path.join(folder_path, 'fc_softmax'))

    def load(self, folder_path):
        self.deepid1.load(os.path.join(folder_path, 'deepid1'))
        self.deepid2.load(os.path.join(folder_path, 'deepid2'))
        self.fc_cross.load(os.path.join(folder_path, 'fc_cross'))
        self.fc_softmax.load(os.path.join(folder_path, 'fc_softmax'))


if __name__ == '__main__':
    train_imgs, train_labels, test_imgs, test_labels = preprocess.get_images()
    fv = FaceVerification()
    fv.train(train_imgs, train_labels, 300)
    fv.save('../saved_model_0.7')
Пример #4
0
        else:
            batch_num = all_batch // max_batch + 1
        print('start iteration....')
        for i in range(iteration_limit):
            print(f'---------> iteration {i}')
            for j in range(batch_num):
                print(f'\t-----> batch {j}')
                start = time.time()
                batch_data = imgs[j * max_batch:(j + 1) * max_batch]
                batch_labels = labes[j * max_batch:(j + 1) * max_batch]
                pred = self.forward(batch_data)
                loss, dy = self.get_loss_and_dy(pred, batch_labels)
                self.backward(dy)
                print(f'\tloss = {loss}, time cost = {time.time() - start} s')
                pred = self.forward(test_imgs)
                print(f'\tF1 = {f1_score(test_labels, self.get_labels(pred))}')


if __name__ == '__main__':
    imgs, labels = preprocess.get_images()

    n = imgs.shape[0]
    split = n - max_batch
    test_imgs = imgs[split:]
    test_labels = labels[split:]
    train_imgs = imgs[:split]
    train_labels = labels[:split]

    fv = FaceVerification()
    fv.train(train_imgs, train_labels, 1000)
Пример #5
0
                    predictedWordIndex = torch.multinomial(x_t[0][0], 1)[0]

                    x_t.new_zeros(x_t.size())
                    x_t[0][0][predictedWordIndex] = 1
                    x_t = self.linear2(x_t)

                    y_pred.append(predictedWordIndex.item())

                return y_pred


trainDataType = 'train2014'
valDataType = 'val2014'

# retrieve images
images = get_images('cocoapi-master', trainDataType)
print('%d Training Images Loaded.' % len(images))
valImages = get_images('cocoapi-master', valDataType)
print('%d Validation Images Loaded.' % len(valImages))

# retrieve captions
captions = get_captions('cocoapi-master', trainDataType, images)
(vocab, vocabEncoding) = create_encoding(captions)
vocabSize = len(vocab)
valCaptions = get_captions('cocoapi-master', valDataType, valImages)

initModel = Captioner(vocabSize, vocabEncoding['<end>'])
initModel.load_state_dict(torch.load('initial_captioner.pt'))
model = Captioner(vocabSize, vocabEncoding['<end>'])
if torch.cuda.is_available():
    initModel.cuda()