<s> from __future__ import absolute_import <EOL> import os <EOL> import re <EOL> import subprocess <EOL> import time <EOL> import tempfile <EOL> import flask <EOL> from . errors import Error , NetworkVisualizationError , BadNetworkError <EOL> from . framework import Framework <EOL> import digits <EOL> from digits import utils <EOL> from digits . config import config_value <EOL> from digits . model . tasks import TorchTrainTask <EOL> from digits . utils import subclass , override <EOL> @ subclass <EOL> class TorchFramework ( Framework ) : <EOL> """<STR_LIT>""" <EOL> NAME = '<STR_LIT>' <EOL> CLASS = '<STR_LIT>' <EOL> CAN_SHUFFLE_DATA = True <EOL> SUPPORTED_SOLVER_TYPES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self ) : <EOL> super ( TorchFramework , self ) . __init__ ( ) <EOL> self . framework_id = self . CLASS <EOL> @ override <EOL> def create_train_task ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return TorchTrainTask ( framework_id = self . framework_id , ** kwargs ) <EOL> @ override <EOL> def get_standard_network_desc ( self , network ) : <EOL> """<STR_LIT>""" <EOL> networks_dir = os . path . join ( os . path . dirname ( digits . __file__ ) , '<STR_LIT>' , self . CLASS ) <EOL> if ( network == "<STR_LIT>" or network == "<STR_LIT>" ) : <EOL> networks_dir = os . path . join ( networks_dir , '<STR_LIT>' ) <EOL> for filename in os . listdir ( networks_dir ) : <EOL> path = os . path . join ( networks_dir , filename ) <EOL> if os . path . isfile ( path ) : <EOL> match = None <EOL> match = re . match ( r'<STR_LIT>' % network , filename ) <EOL> if match : <EOL> with open ( path ) as infile : <EOL> return infile . read ( ) <EOL> return None <EOL> @ override <EOL> def get_network_from_desc ( self , network_desc ) : <EOL> """<STR_LIT>""" <EOL> return network_desc <EOL> @ override <EOL> def get_network_from_previous ( self , previous_network , use_same_dataset ) : <EOL> """<STR_LIT>""" <EOL> return previous_network <EOL> @ override <EOL> def validate_network ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> @ override <EOL> def get_network_visualization ( self , desc ) : <EOL> """<STR_LIT>""" <EOL> temp_network_handle , temp_network_path = tempfile . mkstemp ( suffix = '<STR_LIT>' ) <EOL> os . write ( temp_network_handle , desc ) <EOL> os . close ( temp_network_handle ) <EOL> try : <EOL> if config_value ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> torch_bin = '<STR_LIT>' <EOL> else : <EOL> torch_bin = os . path . join ( config_value ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' ) <EOL> args = [ torch_bin , <EOL> os . path . join ( os . path . dirname ( os . path . dirname ( digits . __file__ ) ) , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' % os . path . splitext ( os . path . basename ( temp_network_path ) ) [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' % os . path . dirname ( temp_network_path ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> p = subprocess . Popen ( args , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT , <EOL> close_fds = True , <EOL> ) <EOL> regex = re . compile ( '<STR_LIT>' , re . UNICODE ) <EOL> collecting_net_definition = False <EOL> desc = [ ] <EOL> unrecognized_output = [ ] <EOL> while p . poll ( ) is None : <EOL> for line in utils . nonblocking_readlines ( p . stdout ) : <EOL> if line is not None : <EOL> line = regex . sub ( '<STR_LIT>' , line ) <EOL> timestamp , level , message = TorchTrainTask . preprocess_output_torch ( line . strip ( ) ) <EOL> if message : <EOL> if message . startswith ( '<STR_LIT>' ) : <EOL> collecting_net_definition = not collecting_net_definition <EOL> else : <EOL> if collecting_net_definition : <EOL> desc . append ( line ) <EOL> elif len ( line ) : <EOL> unrecognized_output . append ( line ) <EOL> else : <EOL> time . sleep ( <NUM_LIT> ) <EOL> if not len ( desc ) : <EOL> raise NetworkVisualizationError ( '<STR_LIT>' . join ( unrecognized_output ) ) <EOL> else : <EOL> output = flask . Markup ( '<STR_LIT>' ) <EOL> for line in desc : <EOL> output += flask . Markup . escape ( line ) <EOL> output += flask . Markup ( '<STR_LIT>' ) <EOL> return output <EOL> finally : <EOL> os . remove ( temp_network_path ) </s>
