The document appears to be a Jupyter notebook that covers various topics in artificial intelligence including perception, language, reasoning, intuition, and creativity. Code cells are included that demonstrate techniques such as neural style transfer, image captioning, text generation with LSTMs, reinforcement learning using DQN, and deep dreaming. Videos are also embedded to provide visual examples of concepts like AlphaGo intuition and AI creativity.
17. 2/5/2019 AIpreso
http://localhost:8888/notebooks/AIpreso.ipynb 17/31
In [2]:
Perception & Language
a quaintlycostumed little couple still marvelling about o
n their own ground floor by the unnecessary light of a gu
ttering candle chapter the furniture that went mad now it
happened that in the early hours of whitmonday before mil
lie was hunted out for the day mr hall and mrs hall both
import os
os.chdir("/home2/innolabs/Dropbox/Common/Augment-Train/Demos")
%run -i lstm_text_gen.py
18. 2/5/2019 AIpreso
http://localhost:8888/notebooks/AIpreso.ipynb 18/31
In [6]:
import os
os.chdir("/home2/innolabs/Dropbox/Common/Augment-Train/Demos")
## Image Caption generation -- using a previously pre-trained model
from pickle import load
from numpy import argmax
from keras.preprocessing.sequence import pad_sequences
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
from keras.models import load_model
def extract_features(filename):
model = VGG16()
# Transfer Learning
model.layers.pop()
model = Model(inputs=model.inputs, outputs=model.layers[-1].output
image = load_img(filename, target_size=(224, 224))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.sh
image = preprocess_input(image)
feature = model.predict(image, verbose=0)
return feature
def word_for_id(integer, tokenizer):
for word, index in tokenizer.word_index.items():
if index == integer:
return word
return None
def cleanup_summary(summary):
index = summary.find('startseq ')
if index > -1:
summary = summary[len('startseq '):]
index = summary.find(' endseq')
if index > -1:
summary = summary[:index]
return summary
# Function to generate the caption for the image
def generate_caption(model, tokenizer, photo, max_length):
in_text = 'startseq'
for _ in range(max_length):
sequence = tokenizer.texts_to_sequences([in_text])[0]
sequence = pad_sequences([sequence], maxlen=max_length)
ytgt = model.predict([photo,sequence], verbose=0)
ytgt = argmax(ytgt)
word = word_for_id(ytgt, tokenizer)
if word is None:
break
in_text += ' ' + word
if word == 'endseq':
break
return in_text
tokenizer = load(open('tokenizer2.pkl', 'rb'))
19. 2/5/2019 AIpreso
http://localhost:8888/notebooks/AIpreso.ipynb 19/31
Reasoning
two dogs are running through the water
max_length = 34
model = load_model('model2.h5') # Load the pre-trained model
photo = extract_features('image.jpeg')
caption = generate_caption(model, tokenizer, photo, max_length)
caption = cleanup_summary(caption)
print(caption)