def train():
    fact_dict = load_pickle()

    sentences, ratings = split(fact_dict)

    plotter, fig, ax = create_plot(["loss", "accuracy"])

    model = fm.Model(dim_input=50, dim_recurrent=100, dim_output=1)
    optimizer = Adam(model.parameters)

    plot_every = 500

    for k in range(100000):
        output = model(sentences)

        loss = softmax_crossentropy(output, ratings)

        acc = float(output.data.squeeze() == ratings.item())

        plotter.set_train_batch({
            "loss": loss.item(),
            "accuracy": acc
        },
                                batch_size=1,
                                plot=False)

        if k % plot_every == 0 and k > 0:
            plotter.set_train_epoch()

        loss.backward()
        optimizer.step()
        loss.null_gradients()
Exemplo n.º 2
0
from mynn.optimizers.adam import Adam
from Model import Model

model = Model()

optim = Adam(model.parameters, learning_rate=1e-04, weight_decay=5e-04)


#notebook with noggin
#plotter, fig, ax = create_plot(('loss', 'reg_loss', 'cls_loss', 'Precision', 'Recall'),
              #                 1, figsize=(8, 12))


path = r"C:\Users\khoui\Nutrient_Detection\Food_Data"
train_dir = r"C:\Users\khoui\Nutrient_Detection\Food_Data\food-11\training"
test_dir = r"C:\Users\khoui\Nutrient_Detection\Food_Data\food-11\validation"

x_train, y_train, x_test, y_test  = load_draw_data(path)

generate_plate_set(x_train, y_train)

#load plate data

generate


for _ in range(5):
    train_epoch(train_data, train_boxes, train_labels, anchor_boxes, model, optim,
                val_data, val_boxes, val_labels)
    #plotter.plot()
Exemplo n.º 3
0
                return weights
            def weights2(self):
                weights=self.dense2.weight.data
                return weights
            def weights3(self):
                weights=self.dense3.weight.data
                return weights
            def weights4(self):
                weights=self.dense4.weight.data
                return weights
            @property
            def parameters(self):
                ''' A convenience function for getting all the parameters of our model. '''
                return self.dense1.parameters + self.dense2.parameters + self.dense3.parameters + self.dense4.parameters
        model = Model()
        optim = Adam(model.parameters)
        # Create the shape-(1000,1) training data
        
        train_data = mg.linspace(0,19,100).reshape(100,1)
        # Create your parameters using your function `create_parameters`; 
        # start off with N=10 for the number of model parameters
        # to use for defining F(x)
        

        # Set `batch_size = 25`: the number of predictions that we will make in each training step

        # Define the function `true_f`, which should just accept `x` and return `np.cos(x)`
        def true_f(x):
            return np.interp(x,np.linspace(0,19,20),unknown_data[BodyPartIndex][CoordIndex])
        batch_size = 10
        for epoch_cnt in range(200000):
Exemplo n.º 4
0
def train(x_tr, y_tr, x_te, y_te):
    x_tr = np.array(x_tr)
    y_tr = np.array(y_tr)
    x_train_vec = vectorize(x_tr, 92)
    x_te = np.array(x_te)
    y_te = np.array(y_te)
    x_test_vec = vectorize(x_te, 58)
    model = Model()
    optim = Adam(model.parameters, learning_rate=1e-4)
    plotter, fig, ax = create_plot(metrics=["loss", "accuracy"])

    batch_size = 50

    for epoch_cnt in range(7):
        idxs = np.arange(len(x_tr))
        np.random.shuffle(idxs)

        for batch_cnt in range(len(x_tr) // batch_size):
            # make slice object so indices can be referenced later
            batch_indices = idxs[slice(batch_cnt * batch_size,
                                       (batch_cnt + 1) * batch_size)]
            #batch = x_train[batch_indices]  # random batch of our training data

            # retrieve glove embeddings for batch
            # initialize every value as small number which will be the placeholder for not found embeddings
            arr = x_train_vec[batch_indices]
            """
            arr = np.ones((len(batch), 200, max(train_max, test_max))) / 1000000
            for i, sent in enumerate(batch):
                for j , word in enumerate(sent):   
                    # retrieve glove embedding for every word in sentence
                    try:
                        arr[i,:,j] = glove_model.get_vector(word.lower())
                    
                    # continue if glove embedding not found
                    except Exception as e:
                        continue
            """

            # pass model through batch and perform gradient descent
            pred = model(arr)
            truth = y_tr[batch_indices]

            loss = binary_cross_entropy(pred[:, 0], truth)
            loss.backward()

            optim.step()
            loss.null_gradients()

            acc = accuracy(pred[:, 0], truth)

            # pass loss and accuracy to noggin for plotting
            plotter.set_train_batch({
                "loss": loss.item(),
                "accuracy": acc
            },
                                    batch_size=batch_size)
            """
                return model
            
            
            
            def test(x_te, y_te, model):     
                # compute test statistics
                #idxs = np.arange(len(x_test))
                
                x_te = np.array(x_te)
                y_te = np.array(y_te)
                x_test_vec = vectorize(x_te, 58)
                logger = LiveLogger()
                idxs = np.arange(len(x_te))
                logger.set_train_batch(dict(metric_a=50., metric_b=5.), batch_size=50)
                batch_size = 50
                
                for epoch_cnt in range(2):
                    idxs = np.arange(len(x_te))
                    np.random.shuffle(idxs)
                """
        idxs = np.arange(len(x_te))
        for batch_cnt in range(0, len(x_te) // batch_size):
            batch_indices = idxs[slice(batch_cnt * batch_size,
                                       (batch_cnt + 1) * batch_size)]
            #batch = x_test[batch_indices]

            # again, find embeddings for batch
            arr = x_test_vec[batch_indices]
            """
            arr = np.ones((len(batch), 200, max(train_max, test_max))) / 1000000
            for i, sent in enumerate(batch):
                for j , word in enumerate(sent):   
                    try:
                        arr[i,:,j] = glove_model.get_vector(word.lower())
                    
                    except Exception as e:
                        continue
            """

            # perform forward pass and find accuracy but DO NOT backprop
            pred = model(arr)
            truth = y_te[batch_indices]
            acc = accuracy(pred[:, 0], truth)

            # log the test-accuracy in noggin
            plotter.set_test_batch({"accuracy": acc}, batch_size=batch_size)

        # plot the epoch-level train/test statistics
        plotter.set_train_epoch()
        plotter.set_test_epoch()
    return model
Exemplo n.º 5
0
def main():
    path = r"glove.6B.50d.txt.w2v"
    glove = KeyedVectors.load_word2vec_format(path, binary=False)

    # loads the json file
    path_to_json = "captions_train2014.json"
    with open(path_to_json, "rb") as f:
        json_data = json.load(f)
    resnet = unpickle.unpickle()

    with open("idfs1.pkl", mode="rb") as idf:
        idfs = pickle.load(idf)
    with open("img_to_caption1.pkl", mode="rb") as cap:
        img_to_caption = pickle.load(cap)
    #with open("img_to_coco1.pkl", mode="rb") as coco:
    #img_to_coco=pickle.load(coco)
    model = Model()

    model.dense1.weight = mg.Tensor(np.load('weight.npy'))
    model.dense1.bias = mg.Tensor(np.load('bias.npy'))
    optim = Adam(model.parameters)

    batch_size = 100
    for epoch_cnt in range(100):

        idxs = list(resnet.keys())
        np.random.shuffle(idxs)
        for batch_cnt in range(0, len(idxs) // batch_size - 1):
            batch_indices = idxs[(batch_cnt * batch_size):((batch_cnt + 1) *
                                                           batch_size)]
            batch_indices2 = idxs[((batch_cnt + 1) *
                                   batch_size):((batch_cnt + 2) * batch_size)]
            # id1 = np.random.choice(list(resnet.keys()))
            # print(id1)
            id1 = batch_indices
            # while id1 == id2:
            id2 = batch_indices2

            # print(type(resnet[id1]),type(img_to_caption[id1][0]),type(resnet[id2]))
            good_image = resnet[id1[0]]
            bad_image = resnet[id2[0]]
            text = embed_text.se_text(img_to_caption[id1[0]][0], glove, idfs)
            for i in id1[1:]:
                good_image = np.vstack((good_image, resnet[i]))
                text = np.vstack(
                    (text, embed_text.se_text(img_to_caption[i][0], glove,
                                              idfs)))

            for i in id2[1:]:
                bad_image = np.vstack((bad_image, resnet[i]))

            sim_to_good = cos_sim.cos_sim(model(good_image), text)
            sim_to_bad = cos_sim.cos_sim(model(bad_image), text)

            # compute the loss associated with our predictions(use softmax_cross_entropy)
            loss = margin_ranking_loss(sim_to_good, sim_to_bad, 1, 0.1)
            # back-propagate through your computational graph through your loss
            loss.backward()

            # compute the accuracy between the prediction and the truth
            acc = accuracy(sim_to_good.data, sim_to_bad.data)
            # execute gradient descent by calling step() of optim
            optim.step()
            # null your gradients
            loss.null_gradients()

    np.save('weight', model.dense1.parameters[0].data)
    np.save('bias', model.dense1.parameters[1].data)
Exemplo n.º 6
0
from mygrad.nnet.layers import max_pool
from mynn.optimizers.adam import Adam
from mygrad.nnet.losses import softmax_crossentropy



#needs import/download of data set
#our train = xtrain
#our truth = ytrain


model = Model()
lr = 1e-2
wd = 1e-04

optimization = Adam(model.parameters, learning_rate=lr, weight_decay=wd)

batch_size = #give number here

for batch_cnt in range(0, len(xtrain) // batch_size):
    batch_indices = idxs[batch_cnt * batch_size: (batch_cnt + 1) * batch_size]
    batch = xtrain[batch_indices]  # random batch of our training data

    # compute the predictions for this batch by calling on model
    predictions = model(batch)
    pass

    truth = ytrain[batch_indices]
    pass

    # compute the loss
Exemplo n.º 7
0
x_train = list(x_train)

for i in range(len(x_train)):
    x_train[i] = np.array(to_glove(x_train[i]))

x_train = np.array(x_train)

print(x_train[0].shape, x_train[1].shape)
print("SHAPEEE: ", x_train.shape)


dim_input = 50
dim_recurrent = 16
dim_output = 2
rnn = RNN(dim_input, dim_recurrent, dim_output)
optimizer = Adam(rnn.parameters)

plotter, fig, ax = create_plot(metrics=["loss"])

batch_size = 20

# Trains the model over 10 epochs.
for epoch_cnt in range(50):
    idxs = np.arange(len(x_train))
    np.random.shuffle(idxs)
    print("training epoch number ", epoch_cnt)

    for batch_cnt in range(0, len(x_train) // batch_size):
        batch_indices = idxs[batch_cnt * batch_size: (batch_cnt + 1) * batch_size]

        old = x_train[batch_indices]
Exemplo n.º 8
0
from img_model import Model
import mygrad as mg
import mynn
import numpy as np
from mynn.layers.dense import dense
from mygrad.nnet import margin_ranking_loss
from mynn.optimizers.adam import Adam
import trainingdata as td
import split_n_stitch as ss
import training_functions as tf

#Call the model to convert the image features to image embeddings.
se_img = Model()
optim = Adam(model.parameters, learning_rate=0.1)

#Unpickle the captions database
with open("captions.pickle", mode="rb") as captiondata:
    captions = dict(pickle.load(captiondata))

#Unpickle the image database
with open("resnet18_features.pkl", mode="rb") as imgdata:
    img = dict(pickle.load(imgdata))

#The captions and image embeddings are used to generate 16000 triples.
#Each triple is in the format of (caption, good image embedding, bad image embedding)

captions, good_images, bad_images = td.generate_training(captions, img, 64000)
#testingtriples = td.generate_training(captions, image_embeds, 16000)

#The number of epochs and the batch size are defined
num_epochs = 500