U
    d                  
   @   s  d dl mZ d dlZd dlZd dlZdZz d dlmZ d dl	m
Z
 dZW n6 ek
r~ Z zdeekrned W 5 dZ[X Y nX d d	l	mZmZmZ d dlZd
d Zee deejd dG dd dejZedkreejdkred ed  e  dS )    )text_formatNF)	caffe_pb2)caffe_translatorTz'caffe'zPyTorch/Caffe2 now requires a separate installation of caffe. Right now, this is not found, so we will skip the caffe translator test.)utils	workspace	test_utilc            	   
   C   s   t rtjdsd S t } t }td}t|	 |  W 5 Q R X td}|
|	  W 5 Q R X dD ]}tj| |d|d\}}tdd}|t| W 5 Q R X |jD ]}t|jt| qtd	tj}td
| t|  qnd S )Ndata/testdata/caffe_translatorz.data/testdata/caffe_translator/deploy.prototxtzAdata/testdata/caffe_translator/bvlc_reference_caffenet.caffemodel)TFT)Zis_testremove_legacy_padzFdata/testdata/caffe_translator/bvlc_reference_caffenet.translatedmodelwz,data/testdata/caffe_translator/data_dump.npydata)CAFFE_FOUNDospathexistsr   ZNetParameteropenr   ZMergereadZParseFromStringr   ZTranslateModelwritestrprotosr   ZFeedBlobnamer   ZCaffe2TensorToNumpyArraynploadZastypeZfloat32Z
RunNetOnceZSerializeToString)	ZcaffenetZcaffenet_pretrainedfr	   netZpretrained_paramsZfidparamr    r   G/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/caffe_translator_test.pysetUpModule   s8    
  


r   zNo Caffe installation found.r   z<No testdata existing for the caffe translator test. Exiting.c                   @   s   e Zd Zdd ZdS )TestNumericalEquivalencec                 C   s   ddddddddd	d
ddddg}|D ]b}t d| t|}td| d }| |j|j t|}tj	j
|| || dd q$d S )NZconv1Zpool1Znorm1Zconv2Zpool2Znorm2Zconv3Zconv4Zconv5Zpool5Zfc6Zfc7Zfc8ZprobzVerifying {}zdata/testdata/caffe_translator/z	_dump.npy   )decimal)printformatr   Z	FetchBlobr   r   assertEqualshapemaxZtestingZassert_almost_equal)selfnamesr   Zcaffe2_result	referenceZscaler   r   r   	testBlobs?   s8                


z"TestNumericalEquivalence.testBlobsN)__name__
__module____qualname__r)   r   r   r   r   r   :   s   r   __main__   ztIf you do not explicitly ask to run this test, I will not run it. Pass in any argument to have the test run for you.)Zgoogle.protobufr   Znumpyr   r   sysr   Zcaffe.protor   Zcaffe2.pythonr   	Exceptioner   r!   r   r   r   Zunittestr   ZskipIfr   r   ZTestCaser   r*   lenargvexitmainr   r   r   r   <module>   s<   

