-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
73 lines (61 loc) · 18.2 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# import numpy as np
# from tensorflow import keras
# from keras.preprocessing import sequence
# from keras.preprocessing import image
# from keras.applications.inception_v3 import InceptionV3
# from keras.applications.inception_v3 import preprocess_input
# from keras.models import Model
# import sys
vocab = ['startseq', 'a', 'child', 'in', 'pink', 'dress', 'is', 'climbing', 'up', 'set', 'of', 'stairs', 'an', 'way', 'endseq', 'girl', 'going', 'into', 'wooden', 'building', 'little', 'the', 'to', 'her', 'black', 'dog', 'and', 'spotted', 'are', 'fighting', 'tricolored', 'playing', 'with', 'each', 'other', 'on', 'road', 'white', 'brown', 'spots', 'staring', 'at', 'street', 'two', 'dogs', 'different', 'looking', 'pavement', 'moving', 'toward', 'covered', 'paint', 'sits', 'front', 'painted', 'rainbow', 'hands', 'bowl', 'sitting', 'large', 'small', 'grass', 'plays', 'it', 'there', 'pigtails', 'painting', 'young', 'outside', 'man', 'lays', 'bench', 'while', 'his', 'by', 'him', 'which', 'also', 'tied', 'sleeping', 'next', 'shirtless', 'lies', 'park', 'laying', 'holding', 'leash', 'ground', 'orange', 'hat', 'something', 'wears', 'glasses', 'wearing', 'beer', 'can', 'ears', 'rope', 'net', 'red', 'climbs', 'bridge', 'onto', 'ropes', 'playground', 'running', 'grassy', 'garden', 'surrounded', 'fence', 'through', 'terrier', 'green', 'runs', 'near', 'shakes', 'its', 'head', 'shore', 'ball', 'edge', 'beach', 'feet', 'stands', 'shaking', 'off', 'water', 'standing', 'turned', 'one', 'side', 'boy', 'smiles', 'wall', 'city', 'overalls', 'working', 'stone', 'walking', 'paved', 'metal', 'pole', 'behind', 'smiling', 'shirt', 'blue', 'jeans', 'rock', 'leaps', 'over', 'log', 'grey', 'leaping', 'fallen', 'tree', 'collar', 'jumping', 'jumped', 'stump', 'snow', 'field', 'surface', 'pictures', 'skier', 'skis', 'past', 'another', 'person', 'looks', 'trees', 'for', 'cliff', 'group', 'people', 'seven', 'climbers', 'face', 'whilst', 'several', 'row', 'watches', 'holds', 'line', 'chases', 'from', 'sprinkler', 'lawn', 'hose', 'away', 'prepares', 'catch', 'thrown', 'object', 'nearby', 'cars', 'about', 'yellow', 'mouth', 'toy', 'ready', 'flying', 'air', 'after', 'get', 'jumps', 'towards', 'trying', 'midair', 'couple', 'infant', 'being', 'held', 'male', 'pond', 'stroller', 'sit', 'baby', 'their', 'under', 'facing', 'lake', 'woman', 'along', 'body', 'outdoors', 'surf', 'lab', 'splashes', 'this', 'splashing', 'hole', 'ice', 'frozen', 'men', 'fishing', 'play', 'making', 'turn', 'sand', 'together', 'tan', 'sandy', 'uses', 'climber', 'jacket', 'pants', 'scaling', 'waterfall', 'carries', 'as', 'he', 'walks', 'carrying', 'has', 'item', 'wet', 'kayak', 'life', 'jackets', 'rowing', 'canoe', 'waters', 'ride', 'courtyard', 'catching', 'chocolate', 'driveway', 'art', 'structure', 'glass', 'reads', 'newspaper', 'sculpture', 'office', 'statue', 'backpack', 'buildings', 'reading', 'tent', 'setting', 'snowy', 'very', 'tall', 'distance', 'camera', 'mountainside', 'out', 'view', 'three', 'overlooking', 'valley', 'hang', 'top', 'big', 'hill', 'rest', 'ledge', 'above', 'down', 'many', 'inflatable', 'boats', 'railing', 'below', 'crowd', 'jersey', 'pose', 'hand', 'some', 'posing', 'picture', 'asian', 'blond', 'background', 'guy', 'biting', 'striped', 'rail', 'crowded', 'takes', 'jump', 'skateboard', 'performing', 'trick', 'leans', 'skateboarder', 'doing', 'board', 'platform', 'skateboarders', 'paddling', 'river', 'seen', 'kayaking', 'paddles', 'boat', 'paddle', 'shallow', 'girls', 'ocean', 'four', 'children', 'pajamas', 'have', 'pillow', 'fight', 'kids', 'bed', 'having', 'workers', 'beam', 'taking', 'break', 'construction', 'take', 'seat', 'train', 'rides', 'book', 'rocky', 'run', 'across', 'area', 'end', 'high', 'diving', 'pool', 'kid', 'swimming', 'window', 'tank', 'door', 'puts', 'putting', 'light', 'hiker', 'mountains', 'ski', 's', 'landscape', 'snowcovered', 'mountain', 'beautiful', 'mountaintop', 'attempting', 'purple', 'low', 'cut', 'yard', 'frisbee', 'shorts', 'parking', 'lot', 'middle', 'during', 'heavy', 'traffic', 'mat', 'between', 'open', 'busy', 'terrain', 'doberman', 'chasing', 'catches', 'tennis', 'watching', 'balloons', 'night', 'hot', 'lit', 'lined', 'helmet', 'bike', 'dirt', 'bicycle', 'race', 'quickly', 'bmx', 'dark', 'throwing', 'bubbles', 'foam', 'ramp', 'getting', 'sticks', 'tongue', 'boys', 'make', 'faces', 'sticking', 'look', 'silly', 'blonde', 'horse', 'sweatshirt', 'fire', 'barrel', 'stand', 'lead', 'horses', 'sheep', 'pushing', 'skinny', 'smaller', 'them', 'various', 'safety', 'harness', 'indoor', 'rocks', 'ring', 'teenage', 'slide', 'forest', 'skiing', 'wooded', 'crosscountry', 'skiers', 'woodland', 'sliding', 'trail', 'woods', 'lone', 'hikers', 'pathway', 'path', 'happily', 'dressed', 'atop', 'pull', 'cart', 'golden', 'coat', 'pulling', 'carriage', 'sled', 'sheer', 'using', 'flat', 'steep', 'vest', 'inside', 'cave', 'shows', 'sunlight', 'lay', 'dry', 'resting', 'gear', 'chalk', 'stream', 'drawing', 'sidewalk', 'deep', 'pile', 'fountain', 'fountains', 'sprayed', 'tunnel', 'course', 'obstacle', 'around', 'swimsuit', 'arms', 'outstretched', 'handrail', 'bathing', 'suit', 'drinking', 'spray', 'soaked', 'jet', 'shower', 'gets', 'underwater', 'redheaded', 'swims', 'alone', 'snowmobile', 'flies', 'rider', 'or', 'machine', 'pine', 'rural', 'riding', 'helmets', 'goggles', 'drive', 'heads', 'atv', 'wheel', 'empty', 'all', 'vehicle', 'airborne', 'soccer', 'arm', 'tshirt', 'artist', 'clouds', 'colors', 'paper', 'cyclist', 'sharp', 'curve', 'cows', 'biker', 'old', 'video', 'lips', 'gun', 'collie', 'audience', 'watch', 'onlookers', 'toddler', 'shoes', 'attached', 'that', 'public', 'wood', 'animal', 'bird', 'eating', 'someone', 'eats', 'finger', 'fast', 'wades', 'laughs', 'colorful', 'outdoor', 'handstand', 'does', 'upside', 'snowboard', 'surfboard', 'bikes', 'traveling', 'motorcycles', 'half', 'naked', 'chair', 'older', 'back', 'relaxes', 'patio', 'where', 'bicycles', 'cap', 'deck', 'close', 'parked', 'relaxing', 'topless', 'floating', 'both', 'mouths', 'tug', 'chew', 'haired', 'drink', 'hair', 'party', 'streets', 'they', 'women', 'parade', 'splash', 'sandals', 'short', 'backpacks', 'cardboard', 'bus', 'station', 'room', 'plants', 'crossing', 'sign', 'beside', 'family', 'gathered', 'van', 'bright', 'truck', 'others', 'helping', 'step', 'pulled', 'brightly', 'poses', 'costume', 'hugs', 'who', 'event', 'hugging', 'bicyclist', 'cross', 'biking', 'lady', 'sky', 'car', 'wait', 'jogging', 'jogs', 'headphones', 'plant', 'store', 'corner', 'bicyclists', 'intersection', 'waiting', 'bikers', 'stop', 'town', 'without', 'wagon', 'leading', 'hits', 'hitting', 'baseball', 'adult', 'put', 'cage', 'spins', 'waves', 'sun', 'she', 'marching', 'band', 'teenager', 'game', 'sneakers', 'leap', 'elderly', 'straw', 'gray', 'sweater', 'walk', 'hallway', 'colored', 'flip', 'flops', 'hood', 'legs', 'boulder', 'clear', 'hooded', 'bending', 'blanket', 'stuffed', 'animals', 'giving', 'toys', 'war', 'floor', 'nose', 'silver', 'closeup', 'bar', 'neon', 'hanging', 'bank', 'computer', 'rough', 'shaped', 'plastic', 'team', 'wear', 'uniforms', 'same', 'hats', 'caps', 'flowers', 'shaggy', 'stick', 'long', 'alongside', 'advertisement', 'subway', 'umbrella', 'unicycle', 'scooter', 'reaches', 'dock', 'pier', 'concrete', 'landing', 'table', 'picnic', 'luggage', 'eat', 'floral', 'purse', 'suspended', 'type', 'strange', 'eyes', 'closed', 'photo', 'outfit', 'photograph', 'just', 'church', 'climb', 'swing', 'swings', 'heels', 'brick', 'against', 'wrestle', 'plain', 'clothes', 'asleep', 'sofa', 'pacifier', 'bear', 'soda', 'vendor', 'drinks', 'print', 'gold', 'stove', 'pipe', 'makeup', 'benches', 'sunglasses', 'mask', 'wetsuit', 'bald', 'flips', 'ear', 'bite', 'number', 'six', 'chased', 'kneeling', 'approaching', 'thin', 'carpet', 'rug', 'lying', 'furry', 'suits', 'friends', 'smile', 'ladies', 'bikinis', 'reflection', 'dances', 'skirt', 'crouches', 'writing', 'blurry', 'descending', 'kicking', 'police', 'motorcycle', 'muddy', 'motorbike', 'uphill', 'motocross', 'racer', 'display', 'underwear', 'wire', 'creek', 'leaning', 'motion', 'says', 'sunset', 'float', 'drives', 'waits', 'driving', 'lap', 'tries', 'playpen', 'female', 'football', 'foot', 'first', 'seated', 'candles', 'birthday', 'place', '5', 'cake', 'trotting', 'few', 'adults', 'splashed', 'sunny', 'day', 'lots', 'shown', 'cloudy', 'khaki', 'passes', 'sideways', 'waterskier', 'performs', 'tricks', 'waterskiing', 'knee', 'begins', 'fall', 'wakeboarding', 'younger', 'fish', 'stare', 'pitch', 'uniform', 'player', 'backyard', 'stuck', 'turning', 'wide', 'barks', 'shoulder', 'fluffy', 'grab', 'tables', 'talks', 'showing', 'talking', 'phone', 'cellphone', 'flower', 'attire', 'device', 'american', 'flag', 'hiking', 'hikes', 'underneath', 'stool', 'chairs', 'gives', 'eye', 'trunks', 'reflective', 'swim', 'block', 'dirty', 'bottle', 'checkered', 'tight', 'bars', 'hangs', 'monkey', 'jungle', 'gym', 'ladder', 'warm', 'swinging', 'wings', 'slides', 'floaties', 'hit', 'jean', 'railroad', 'tracks', 'barefoot', 'bearded', 'peace', 'stops', 'feather', 'shaved', 'shopping', 'filled', 'wave', 'sports', 'attempt', 'five', 'tire', 'racers', 'racing', 'laughing', 'bucket', 'beneath', 'boogie', 'trampoline', 'puppies', 'chase', 'heavily', 'cold', 'but', 'sets', 'balloon', 'figure', 'right', 'slightly', 'floats', 'bikini', 'huge', 'scene', 'skimpy', 'foreground', 'skating', 'helps', 'skate', 'hold', 'go', 'rink', 'museum', 'retriever', 'kissing', 'cheek', 'tie', 'kiss', 'happy', 'shirts', 'vests', 'formal', 'balcony', 'amusement', 'carnival', 'swords', 'bat', 'cup', 'rubber', 'referee', 'funny', 'bushes', 'matching', 'denim', 'gravel', 'land', 'puppy', 'beige', 'left', 'frame', 'help', 'push', 'among', 'cement', 'shop', 'retrieves', 'golf', 'hind', 'taken', 'balls', 'no', 'fetch', 'spectators', 'pitbull', 'spraying', 'jack', 'marker', 'house', 'mirror', 'hoodie', 'softball', 'throw', 'playfully', 'tossing', 'throws', 'marked', 'track', 'circle', 'camping', 'equipment', 'bags', 'bottom', 'base', 'surfing', 'made', 'dune', 'snowboarding', 'poodle', 'full', 'bath', 'cloth', 'wading', 'towel', 'formation', 'walkway', 'enjoying', 'wine', 'screen', 'laptop', 'snowboarder', 'cover', 'sheet', 'females', 'clothing', 'case', 'outfits', 'dresses', 'fishes', 'tops', 'these', 'you', 'plaid', 'bag', 'speaks', 'coffee', 'restaurant', 'be', 'only', 'retrieving', 'pack', 'father', 'peak', 'like', 'cowboy', 'chain', 'neck', 'roller', 'coaster', 'shot', 'poles', 'slope', 'innertube', 'tube', 'boots', 'brunette', 'curb', 'lift', 'rolling', 'competing', 'agility', 'palm', 'touch', 'gentleman', 'lean', 'second', 'themselves', 'waist', 'pirate', 'patch', 'bandanna', 'makes', 'headscarf', 'catcher', 'points', 'pointing', 'sides', 'teams', 'players', 'volleyball', 'athletic', 'stares', 'preparing', 'forward', 'necklace', 'trunk', 'weather', 'guys', 'touches', 'brother', 'reaching', 'gloves', 'food', 'speaking', 'box', 'hurdle', 'pulls', 'cigarette', 'fisherman', 'mohawk', 'style', 'shade', 'ribbon', 'karate', 'martial', 'arts', 'practicing', 'enjoys', 'german', 'piece', 'handles', 'touching', 'graffiti', 'skateboarding', 'skater', 'amidst', 'cloud', 'kitchen', 'meal', 'spread', 'tattoos', 'backs', 'bathroom', 'mother', 'feeding', '2', 'watched', 'raising', 'almost', 'partially', 'shoulders', 'beard', 'disc', 'try', 'attempts', 'distant', 'scales', 'fun', 'multicolored', 'branch', 'chewing', 'leafy', 'teeth', 'bleachers', 'third', 'teen', 'school', 'gate', 'crashing', 'rapids', 'cyclists', 'bottles', 'branches', 'summer', 'vehicles', 'bites', 'steps', 'bouncing', 'enclosed', 'balancing', 'leg', 'see', 'gestures', 'though', 'emerges', 'kayaker', 'goes', 'rain', 'jeep', 'training', 'pale', 'downhill', 'court', 'visible', 'counter', 'murky', 'rafting', 'tires', 'leaves', 'longhaired', 'bounds', 'skateboards', 'do', 'mud', 'crosswalk', 'not', 'so', 'narrow', 'hills', 'autumn', 'bare', 'scuba', 'diver', 'coats', 'ahead', 'petting', 'farm', 'fenced', 'pen', 'goat', 'curly', 'upsidedown', 'flipping', 'gather', 'wrestling', 'horizon', 'kite', 'countryside', 'fly', 'wheelie', 'homeless', 'couch', 'scarf', 'beads', 'slip', 'barren', 'skull', 'dust', 'blowing', 'wind', 'urban', 'digging', 'brush', 'kneels', 'string', 'rests', 'pouring', 'hay', 'fur', 'listening', 'fair', 'image', 'covering', 'mound', 'raised', 'raises', 'cricket', 'singing', 'sings', 'microphone', 'guitar', 'crawls', 'knees', 'snowball', 'fabric', 'kicks', 'teammate', 'puddle', 'opposite', 'lights', 'corn', 'photographer', 'driver', 'ridden', 'professional', 'aqua', 'country', 'flags', 'boardwalk', 'link', 'silhouette', 'rows', 'students', 'sniffs', 'sniffing', 'hoop', 'motorcyclist', 'speed', 'racetrack', 'position', 'sea', 'camouflage', 'bunch', 'picking', 'surfer', 'surfs', 'foggy', 'dreadlocks', 'flowered', 'staircase', 'embrace', 'goal', 'goalie', 'uniformed', 'fake', 'races', 'balances', 'balance', 'himself', 'tricycle', 'following', 'hike', 'tag', 'stomach', 'headfirst', 'shoreline', 'cat', 'stripes', 'swimmer', 'skinned', 'neighborhood', 'larger', 'living', 'leashes', 'reach', 'costumes', 'music', 'sprinklers', 'logs', 'itself', 'single', 'time', 'spiderman', 'candy', 'polka', 'dot', 'hardhat', 'carry', 'coming', 'stadium', 'licking', 'medium', 'sized', 'lounge', 'cream', 'follows', 'owner', 'pit', 'still', 'plate', 'dead', 'winter', 'muzzle', 'blow', 'hoops', 'arcade', 'home', 'wrapped', 'work', 'caught', 'spinning', 'turns', '4', 'appears', 'markings', 'basketball', 'rollerblading', 'inline', 'rollerblades', 'skates', 'enjoy', 'basket', 'desert', 'space', 'porch', 'hillside', 'foliage', 'indoors', 'teenagers', 'talk', 'moves', 'stage', 'onstage', 'perform', 'snowsuit', 'licks', 'poodles', 'raft', 'parachute', 'gathering', 'duck', 'collars', 'cape', 'greyhound', 'greyhounds', 'dusk', 'surrounding', 'decorated', 'riders', 'hug', 'christmas', 'headband', 'friend', 'starting', 'money', 'market', 'grocery', 'direction', 'range', 'casting', 'chest', 'protective', 'identical', 'breaking', 'icy', 'muzzled', 'buckets', 'huddle', 'falling', 'tail', 'rear', 'broken', 'army', 'knit', 'shepherd', 'cow', 'bull', 'ponytail', 'what', 'rolls', 'paws', 'snowcapped', 'crouching', 'giant', 'before', 'toilet', 'action', 'pitcher', 'pigeons', 'birds', 'barrier', 'laugh', 'wheels', 'mostly', '3', 'swimmers', 'swimsuits', 'smoking', 'crossed', 'herself', 'dives', 'santa', 'african', 'desk', 'parka', 'rowboat', 'roof', 'worker', 'cheerleaders', 'waving', 'stunt', 'move', 'flock', 'pair', 'i', 'grinding', 'booth', 'barn', 'signs', 'backwards', '8', 'photographed', 'pass', 'business', 'guard', 'officer', 'bow', 'target', 'leads', 'hurdles', 'dancing', 'fancy', 'violin', 'doorway', 'eastern', 'tiger', 'tattoo', 'indian', 'native', 'hockey', 'surfers', 'airplane', 'cones', 'cone', 'flames', 'fingers', 'leaving', 'muzzles', 'sledding', 'shoveling', 'shovel', 'japanese', 'class', 'passing', 'instrument', 'mug', 'smokes', 'square', 'dance', 'meadow', 'members', 'umbrellas', 'boxing', 'sleeps', 'kisses', 'leather', 'cats', 'blows', 'barking', 'speeds', 'wedding', 'bride', 'comes', 'grabbing', 'dribbles', 'robe', 'hard', 'horseback', 'colourful', 'fetching', 'crosses', 'drums', 'instruments', 'drum', 'bounce', 'shadow', 'kick', 'straight', 'glove', 'batman', 'fireplace', 'wrestler', 'bush', 'natural', 'digs', 'part', 'alley', 'played', 'rollerskating', 'been', 'railings', 'musicians', 'pet', 'flute', 'falls', 'point', 'robes', 'pushes', 'competition', 'numbered', 'thumbs', 'racquet', 'racket', 'ducks', 'geese', 'either', 'ship', 'skyline', 'bend', 'round', 'match', 'plaza', 'apron', 'tulips', 'oklahoma', 'sooners', 'tackle', 'coach', 'strip', 'fans', 'tackled', 'jerseys', 'tackling', 'cheerleader', 'blocks', 'opposing', 'center', 'compete', 'appear', 'runners', 'crying', 'flight', 'bathtub', 'monument', 'protest', 'hospital', 'cast', 'gallery', 'wheelchair', 'show', 'jumper', 'jockey', 'blurred', 'cafe', 'banner', 'how', 'merrygoround', 'tripod', 'photographs', 'camel', 'shooting', 'bubble', 'ribbons', 'sing', 'hula', 'advertising', 'smoke', 'trashcan', 'skirts', 'followed', 'tutu', 'boarding', 'aged', 'apple', 'beagle', 'well', 'shoot', 'shoe', 'bunny', 'tents', 'traditional', 'control', 'crouched', 'tuxedos', 'gliding', 'sail', 'chews', 'clown', 'kiddie', 'athlete', 'rollerblader', 'grinds', 'sport', 'rugby', 'policeman', 'rodeo', 'stretches', 'stair', 'ridge', 'runner', 'accordion', 'officers', 'miami', 'crane', 'houses', 'eight', 'puck', 'obama', 'bone', 'swan', 'plane', 'jockeys', 'masks', 'groom', 'mural', 'daughter', 'deer', 'scarves', 'fruit', 'military', 'elephant', 'wrestlers', 'cards', 'sumo', 'camels']
max_length = 38
ixtoword = {}
wordtoix = {}
ix = 1
for w in vocab:
wordtoix[w] = ix
ixtoword[ix] = w
ix += 1
vocab_size = len(ixtoword) + 1
model =keras.models.load_model('model.h5')
def beam_search_predictions(image, beam_index = 7):
start = [wordtoix["startseq"]]
start_word = [[start, 0.0]]
while len(start_word[0][0]) < max_length:
temp = []
for s in start_word:
par_caps = sequence.pad_sequences([s[0]], maxlen=max_length, padding='post')
preds = model.predict([image,par_caps], verbose=0)
word_preds = np.argsort(preds[0])[-beam_index:]
# Getting the top <beam_index>(n) predictions and creating a
# new list so as to put them via the model again
for w in word_preds:
next_cap, prob = s[0][:], s[1]
next_cap.append(w)
prob += preds[0][w]
temp.append([next_cap, prob])
start_word = temp
# Sorting according to the probabilities
start_word = sorted(start_word, reverse=False, key=lambda l: l[1])
# Getting the top words
start_word = start_word[-beam_index:]
start_word = start_word[-1][0]
intermediate_caption = [ixtoword[i] for i in start_word]
final_caption = []
for i in intermediate_caption:
if i != 'endseq':
final_caption.append(i)
else:
break
final_caption = ' '.join(final_caption[1:])
return final_caption
pic = sys.argv[1]
modeli = InceptionV3(weights='imagenet')
model_new = Model(modeli.input, modeli.layers[-2].output)
img = image.load_img(pic, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
fea_vec = model_new.predict(x)
fea_vec = np.reshape(fea_vec, fea_vec.shape[1])
res = fea_vec.reshape((1,2048))
print(beam_search_predictions(res, beam_index = 3))