28.04.2020 Views

Sách Deep Learning cơ bản

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

17.6 Python code 215

import matplotlib.pyplot as plt

# %matplotlib inline

import string

import os

from PIL import Image

import glob

from pickle import dump, load

from time import time

from keras.preprocessing import sequence

from keras.models import Sequential

from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector,\

Activation, Flatten, Reshape, concatenate, \

Dropout, BatchNormalization

from keras.optimizers import Adam, RMSprop

from keras.layers.wrappers import Bidirectional

from keras.layers.merge import add

from keras.applications.inception_v3 import InceptionV3

from keras.preprocessing import image

from keras.models import Model

from keras import Input, layers

from keras import optimizers

from keras.applications.inception_v3 import preprocess_input

from keras.preprocessing.text import Tokenizer

from keras.preprocessing.sequence import pad_sequences

from keras.utils import to_categorical

# Đọc file các caption

def load_doc(filename):

# open the file as read only

file = open(filename, 'r')

# read all text

text = file.read()

# close the file

file.close()

return text

filename = "Flickr8k/Flickr8k_text/Flickr8k.token.txt"

doc = load_doc(filename)

print(doc[:300])

# Lưu caption dưới dạng key value:

#id_image : ['caption 1', 'caption 2', 'caption 3',' caption 4', 'caption 5']

def load_descriptions(doc):

mapping = dict()

# process lines

for line in doc.split('\n'):

# split line by white space

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!