Exemplo n.º 1
0
    def __call__(self, x, y, norm):
        x = self.generator(x)
        loss = self.criterion(x.contiguous().view(-1, x.size(-1)),
                              y.contiguous().view(-1)) / norm
        loss.backward()
        if self.opt is not None:
            self.opt.step()
            self.opt.optimizer.zero_grad()
        return loss.data * norm


# Train the simple copy task.
V = 11
criterion = LabelSmoothing(size=V, padding_idx=0, smoothing=0.0)
model = make_model(V, V, N=2)
model_opt = NoamOpt(
    model.src_embed[0].d_model, 1, 400,
    torch.optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9))

for epoch in range(10):
    model.train()
    run_epoch(data_gen(V, 30, 20), model,
              SimpleLossCompute(model.generator, criterion, model_opt))
    model.eval()
    print(
        run_epoch(data_gen(V, 30, 5), model,
                  SimpleLossCompute(model.generator, criterion, None)))


def greedy_decode(model, src, src_mask, max_len, start_symbol):
Exemplo n.º 2
0
	def __init__(self):
		self._model = train.make_model()
		self._model.compile(loss='mean_squared_error', optimizer='adadelta')
Exemplo n.º 3
0
	def __init__(self):
		self._model = train.make_model()
		self._model.compile(loss='mean_squared_error', optimizer='adadelta')
Exemplo n.º 4
0
#Load Model and Generate Predictions

import tensorflow as tf
from decode_predictions import DecodePredictions
from train import make_model
from utils import resize_and_pad_image
from dataset import data
import tensorflow_datasets as tfds
from utils import visualize_detections

# Change this to `model_dir="retinanet/"` when not using the downloaded weights
weights_dir = "data"

model = make_model()
latest_checkpoint = tf.train.latest_checkpoint(weights_dir)
model.load_weights(latest_checkpoint)

image = tf.keras.Input(shape=[None, None, 3], name="image")
predictions = model(image, training=False)
detections = DecodePredictions(confidence_threshold=0.5)(image, predictions)
inference_model = tf.keras.Model(inputs=image, outputs=detections)


def prepare_image(image):
    image, _, ratio = resize_and_pad_image(image, jitter=None)
    image = tf.keras.applications.resnet.preprocess_input(image)
    return tf.expand_dims(image, axis=0), ratio


_, _, dataset_info = data()
Exemplo n.º 5
0
    ])
    img_resize = resize(img)
    img_for_pred = norm(img_resize).unsqueeze(dim=0)
    attns = []
    start = time.time()
    text = predict(model, pe, img_for_pred, max_len, attns=attns)[0]
    end = time.time()
    text = text[:text.find('[s]')]
    print(f"time consumed:{time_interval(end - start)}")
    for i, char in enumerate(text):
        if torch.cuda.is_available():
            attn = attns[i].cpu()
        attn = attn.detach().numpy()
        show(img_resize, char, attn)
    show(img_resize, text)


if __name__ == '__main__':
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model, pe = make_model()
    model.load_state_dict(
        torch.load(f"./model_params_{2.0}.pkl", map_location="cpu"))
    if torch.cuda.is_available():
        model = model.cuda()
        pe = pe.cuda()

    image = cv2.imread(
        "/data1/luofuyou/ReCTS_test/test_ReCTS_task2_000229.jpg")
    image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    visualize(model, pe, image)