Exemplo n.º 1
0
        #forward, loss, backward, step
        output = encoder(images=images, rotation=rotation, mode=1)

        loss_1 = noise_contrastive_estimator(representations,
                                             output[1],
                                             index,
                                             memory,
                                             negative_nb=negative_nb)
        loss_2 = noise_contrastive_estimator(representations,
                                             output[0],
                                             index,
                                             memory,
                                             negative_nb=negative_nb)
        loss = loss_weight * loss_1 + (1 - loss_weight) * loss_2

        loss.backward()
        optimizer.step()

        #update representation memory
        memory.update(index, output[0].detach())

        # update metric and bar
        train_loss.update(loss.item(), images.shape[0])
        bar.update(step, values=[('train_loss', train_loss.return_avg())])

        #save model if improved
        checkpoint.save_model(encoder, optimizer, train_loss.return_avg(),
                              epoch)

    logger.update(epoch, train_loss.return_avg())
Exemplo n.º 2
0
        optimizer.zero_grad()

        #forward, loss, backward, step
        output = net(images=images, patches=patches, mode=1)

        loss_1 = noise_contrastive_estimator(representations,
                                             output[1],
                                             index,
                                             memory,
                                             negative_nb=negative_nb)
        loss_2 = noise_contrastive_estimator(representations,
                                             output[0],
                                             index,
                                             memory,
                                             negative_nb=negative_nb)
        loss = loss_weight * loss_1 + (1 - loss_weight) * loss_2

        loss.backward()
        optimizer.step()

        # update representation memory
        memory.update(index, output[0].detach().cpu().numpy())

        # update metric and bar
        train_loss.update(loss.item(), images.shape[0])
        bar.update(step, values=[('train_loss', train_loss.return_avg())])
    logger.update(epoch, train_loss.return_avg())

    # save model if improved
    checkpoint.save_model(net, train_loss.return_avg(), epoch)