def test_generate_captions(): ''' Wherein we test test_mod.generate_captions. Since you may use pre-computed weights from any source only print the generated sentence to stdout and check that it is nonempty ''' encoded_img=ei.encodings(ei.model_gen(),test_img) caption=generate_captions(sd,model,encoded_img,beam_size=3) def report(): print('The model generated the caption: '+caption) atexit.register(report) assert(len(caption)>0)
def test_encodings(): ''' We test encode_image.encodings(), which is just a wrapper for VGG16. This proceeds in the same fashion as in test_vgg16(). The truth value for the encodings differ slightly, since the encodings() wrapper uses another preprocessing routine defined in imagenet_utils. ''' model=model_gen() preds=encodings(model=model,path=os.path.join(image_folder,test_image)) encoding_pred_slice=np.array([0,0,0,0,6.943786,0,0,0,0,0.495808,0,0,1.2099614,0,0,1.9550853,0,2.5830698,0,0,1.5520213,6.7467823,0.30691597,0.6208435,0,3.8465405,3.7862554,2.3970299,0,0,3.5254822,2.7294598,0,0,2.7226853,0,3.2338202,2.3976898,0,6.3592043,0,2.7090664,0,0,10.004378,0,0,0,3.0425727,2.0538316,1.8156273,0.15581878,2.3381875,0.88823074,0,0,0,0,0,0,0.036334604,0,0,0,3.5556676,0.29299664,0,0,0,0,0,0,1.0033253,0,0,0,0.96017045,0,5.8062425,0,4.4312,0,0,0,0,0,0,0,2.7901797,0.5715834,0.76234996,1.7294867,1.2244358,0,0,0,0,0,0,0,]) assert(preds.shape==(4096,)) assert(len(preds[preds!=0])==1351) np.testing.assert_allclose(preds[:100],encoding_pred_slice,err_msg='encoding wrapper yielded wrong encoding for test image!')
def text(img): t1= time.time() encode = ei.model_gen() weight = 'Output/Weights.h5' sd = SceneDesc.scenedesc() model = sd.create_model(ret_model = True) model.load_weights(weight) image_path = img encoded_images = ei.encodings(encode, image_path) image_captions = tm.generate_captions(sd, model, encoded_images, beam_size=3) engine = pyttsx.init() print (image_captions) engine.say( str(image_captions)) engine.runAndWait()
def text(img): t1 = time.time() encode = ei.model_gen() weight = 'Output/Weights.h5' sd = SceneDesc.scenedesc() model = sd.create_model(ret_model=True) model.load_weights(weight) image_path = img encoded_images = ei.encodings(encode, image_path) image_captions = tm.generate_captions(sd, model, encoded_images, beam_size=3) engine = pyttsx.init() print '\nCaption Generated for the above Image is=\n', image_captions
def text(caption): encode = ei.model_gen() weight = 'Output/Weights.h5' decoder = decode_caption.decoder() model = decoder.model_gen(ret_model=True) model.load_weights(weight) bleu_sum = 0.0 count = 0 cc = SmoothingFunction() test_imgs_id = open("Flickr8K_Text/Flickr_8k.testImages.txt").read().split( '\n')[:-1] for img_id in test_imgs_id: image_path = "Flickr8K_Data/" + img_id encoded_images = ei.encodings(encode, image_path) image_captions = generate_captions(decoder, model, encoded_images, beam_size=3) print(image_captions) # bleuscore-4 bleus = [] image_captions = image_captions.split() for true_sentense in caption[img_id]: true_sentense = true_sentense.split() try: bleu = sentence_bleu([true_sentense], image_captions, weights=(0.25, 0.25, 0.25, 0.25), smoothing_function=cc.method4) bleus.append(bleu) except: pass if len(bleus) > 0: print(np.mean(bleus)) bleu_sum += np.mean(bleus) count += 1 print(bleu_sum / count)