Pytorch เป็น Keras Model Converter
pip install pytorch2keras
หากต้องการใช้ตัวแปลงอย่างถูกต้องโปรดทำการเปลี่ยนแปลงใน ~/.keras/keras.json ของคุณ:
...
"backend" : " tensorflow " ,
"image_data_format" : " channels_first " ,
... สำหรับการแปลงที่เหมาะสมเป็นรูปแบบ tensorflow.js โปรดใช้ names='short'
นี่คือคำแนะนำสั้น ๆ วิธีรับรุ่น tensorflow.js:
k_model = pytorch_to_keras ( model , input_var , [( 10 , 32 , 32 ,)], verbose = True , names = 'short' ) tensorflowjs_converter แต่บางครั้งก็ไม่ทำงาน เป็นทางเลือกคุณอาจได้รับกราฟ tensorflow และบันทึกเป็นรุ่นแช่แข็ง: # Function below copied from here:
# https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pb
def freeze_session ( session , keep_var_names = None , output_names = None , clear_devices = True ):
"""
Freezes the state of a session into a pruned computation graph.
Creates a new computation graph where variable nodes are replaced by
constants taking their current value in the session. The new graph will be
pruned so subgraphs that are not necessary to compute the requested
outputs are removed.
@param session The TensorFlow session to be frozen.
@param keep_var_names A list of variable names that should not be frozen,
or None to freeze all the variables in the graph.
@param output_names Names of the relevant graph outputs.
@param clear_devices Remove the device directives from the graph for better portability.
@return The frozen graph definition.
"""
from tensorflow . python . framework . graph_util import convert_variables_to_constants
graph = session . graph
with graph . as_default ():
freeze_var_names =
list ( set ( v . op . name for v in tf . global_variables ()). difference ( keep_var_names or []))
output_names = output_names or []
output_names += [ v . op . name for v in tf . global_variables ()]
input_graph_def = graph . as_graph_def ()
if clear_devices :
for node in input_graph_def . node :
node . device = ""
frozen_graph = convert_variables_to_constants ( session , input_graph_def ,
output_names , freeze_var_names )
return frozen_graph
from keras import backend as K
import tensorflow as tf
frozen_graph = freeze_session ( K . get_session (),
output_names = [ out . op . name for out in k_model . outputs ])
tf . train . write_graph ( frozen_graph , "." , "my_model.pb" , as_text = False )
print ([ i for i in k_model . outputs ])my_model.pb เป็นรุ่น TFJS: tensorflowjs_converter
--input_format=tf_frozen_model
--output_node_names= ' TANHTObs/Tanh '
my_model.pb
model_tfjs const MODEL_URL = `model_tfjs/tensorflowjs_model.pb` ;
const WEIGHTS_URL = `model_tfjs/weights_manifest.json` ;
const model = await tf . loadFrozenModel ( MODEL_URL , WEIGHTS_URL ) ; มันเป็นตัวแปลงของกราฟ Pytorch เป็นรุ่น Keras (Tensorflow Backend)
ประการแรกเราต้องโหลด (หรือสร้าง) รุ่น pytorch ที่ถูกต้อง:
class TestConv2d ( nn . Module ):
"""
Module for Conv2d testing
"""
def __init__ ( self , inp = 10 , out = 16 , kernel_size = 3 ):
super ( TestConv2d , self ). __init__ ()
self . conv2d = nn . Conv2d ( inp , out , stride = 1 , kernel_size = kernel_size , bias = True )
def forward ( self , x ):
x = self . conv2d ( x )
return x
model = TestConv2d ()
# load weights here
# model.load_state_dict(torch.load(path_to_weights.pth))ขั้นตอนต่อไป - สร้างตัวแปรจำลองที่มีรูปร่างที่ถูกต้อง:
input_np = np . random . uniform ( 0 , 1 , ( 1 , 10 , 32 , 32 ))
input_var = Variable ( torch . FloatTensor ( input_np ))เราใช้ตัวแปรจำลองเพื่อติดตามโมเดล (ด้วย jit.trace):
from pytorch2keras import pytorch_to_keras
# we should specify shape of the input tensor
k_model = pytorch_to_keras ( model , input_var , [( 10 , 32 , 32 ,)], verbose = True ) นอกจากนี้คุณยังสามารถตั้งค่ามิติ H และ W เป็นไม่มีการสร้างรูปแบบของคุณเป็นตัวไม่ชาญฉลาด (เช่น netowrk แบบ convolutional อย่างเต็มที่):
from pytorch2keras . converter import pytorch_to_keras
# we should specify shape of the input tensor
k_model = pytorch_to_keras ( model , input_var , [( 10 , None , None ,)], verbose = True ) นั่นคือทั้งหมด! หากโมดูลทั้งหมดได้รับการแปลงอย่างถูกต้องโมเดล keras จะถูกเก็บไว้ในตัวแปร k_model
นี่คือวิธีเดียว pytorch_to_keras จากโมดูล pytorch2keras
def pytorch_to_keras (
model , args , input_shapes = None ,
change_ordering = False , verbose = False , name_policy = None ,
):ตัวเลือก:
model - โมเดล pytorch (nn.module) เพื่อแปลง;args - รายการตัวแปรจำลองที่มีรูปร่างที่เหมาะสมinput_shapes - (ทดลอง) รายการที่มีรูปร่าง overrided สำหรับอินพุต;change_ordering - (ทดลอง) บูลีนถ้าเปิดใช้งานตัวแปลงจะพยายามเปลี่ยน BCHW เป็น BHWCverbose - บูลีนบันทึกรายละเอียดของการแปลงname_policy - ตัวเลือก (ทดลอง) จาก [ keep , short , random ] ตัวเลือกตั้งค่านโยบายการตั้งชื่อเลเยอร์เป้าหมาย การเปิดใช้งาน:
ค่าคงที่
convolutions:
องค์ประกอบที่ชาญฉลาด:
เป็นเส้นตรง
การปกติ:
การรวมกลุ่ม:
ดูที่ไดเรกทอรี tests
ซอฟต์แวร์นี้ครอบคลุมโดยใบอนุญาต MIT