Exemple #1
0
    def init_from_config(self, props):
        """
        将props中的用户设置读入
        """
        super(RNNStrategy, self).init_from_config(props)
        # 标的
        self.symbol = props.get('symbol').split(',')
        self.benchmark_symbol = self.symbol[-1]

        # 初始资金
        self.init_balance = props.get('init_balance')
        # 最大持仓股票数
        self.holding_count = props.get('holding_Count')
        self.balance = self.init_balance
        # 初始每支股票购买资金
        self.stock_value = self.balance / self.holding_count

        self.window = 37

        # 固定长度的价格序列
        for s in self.symbol:
            self.price_arr[s] = pd.DataFrame(columns=['low', 'high', 'close'])
            self.pos[s] = 0
            self.holding_day[s] = 0
        create_graph('F:/Code/buysell/slim/macd_j/frozen_graph.pb')
    def __init__(self):
        self._session = tf.Session()

        classify_image.create_graph()

        self.node_name = 'rms_tf_node'

        rospy.init_node(self.node_name, anonymous=True)

        self.monitor = node_monitor(self.node_name)

        self.bridge = CvBridge()

        self._sub = rospy.Subscriber('/logitech_c922/image_raw',
                                     Image,
                                     self.callback,
                                     queue_size=1)

        self._pub = rospy.Publisher('result', String, queue_size=1)

        self._pub1 = rospy.Publisher('Imgnet_Tf', Image, queue_size=1)

        self.score_threshold = rospy.get_param('~score_threshold', 0.1)

        self.use_top_k = rospy.get_param('~use_top_k', 5)

        self.human_string = ''

        self.score = 0
Exemple #3
0
    def __init__(self):
        classify_image.maybe_download_and_extract()
        self._session = tf.Session()
        classify_image.create_graph()
        self._cv_bridge = CvBridge()

        self._sub = rospy.Subscriber('image',
                                     Image,
                                     self.callback,
                                     queue_size=1)
        self._pub = rospy.Publisher('result', String, queue_size=1)
        self.score_threshold = rospy.get_param('~score_threshold', 0.1)
        self.use_top_k = rospy.get_param('~use_top_k', 5)

        def callback(self, image_msg):
            cv_image = self._cv_bridge.imgmsg_to_cv2(image_msg, "bgr8")
            # copy from
            # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/imagenet/classify_image.py
            image_data = cv2.imencode('.jpg', cv_image)[1].tostring()
            # Creates graph from saved GraphDef.
            softmax_tensor = self._session.graph.get_tensor_by_name(
                'softmax:0')
            predictions = self._session.run(
                softmax_tensor, {'DecodeJpeg/contents:0': image_data})
            predictions = np.squeeze(predictions)
            # Creates node ID --> English string lookup.
            node_lookup = classify_image.NodeLookup()
            top_k = predictions.argsort()[-self.use_top_k:][::-1]
            for node_id in top_k:
                human_string = node_lookup.id_to_string(node_id)
                score = predictions[node_id]
                if score > self.score_threshold:
                    rospy.loginfo('%s (score = %.5f)' % (human_string, score))
                    self._pub.publish(human_string)
Exemple #4
0
def main(argv=None):
    # Download and extract model tar file function.
    classify_image.maybe_download_and_extract()

    # Creates a graph from saved GraphDef file and returns a saver.
    classify_image.create_graph()

    basedir = os.path.dirname(__file__)
    with tf.Session() as sess:
        pool3 = sess.graph.get_tensor_by_name('pool_3:0')
        jpeg_data = tf.placeholder(tf.string)
        thumbnail = tf.cast(tf.image.resize_images(tf.image.decode_jpeg(jpeg_data, channels=3), [100, 100]), tf.uint8)
        outputs = []
        files = []
        images = []
        for filename in os.listdir('images'):
            if not filename.endswith('.jpg'):
                continue
            print('process %s...' % filename)
            files.append(filename)
            with open(os.path.join(basedir, 'images', filename), 'rb') as f:
                data = f.read()
                results = sess.run([pool3, thumbnail], {'DecodeJpeg/contents:0': data, jpeg_data: data})
                outputs.append(results[0])
                images.append(results[1])

        embedding_var = tf.Variable(tf.stack([tf.squeeze(x) for x in outputs], axis=0), trainable=False, name='pool3')

        # prepare projector config
        config = projector.ProjectorConfig()
        embedding = config.embeddings.add()
        embedding.tensor_name = embedding_var.name
        summary_writer = tf.summary.FileWriter(os.path.join(basedir, 'logdir'))

        #link metadata
        metadata_path = os.path.join(basedir, 'logdir', 'metadata.tsv')
        with open(metadata_path, 'w') as f:
            for name in files:
                f.write('%s\n' % name)
        embedding.metadata_path = metadata_path

        # write to sprite image file
        image_path = os.path.join(basedir, 'logdir', 'sprite.jpg')
        size = int(math.sqrt(len(images))) + 1
        while len(images) < size * size:
            images.append(np.zeros((100, 100, 3), dtype=np.uint8))
        rows = []
        for i in range(size):
            rows.append(tf.concat(images[i * size:(i + 1) * size], 1))
        jpeg = tf.image.encode_jpeg(tf.concat(rows, 0))
        with open(image_path, 'wb') as f:
            f.write(sess.run(jpeg))
        embedding.sprite.image_path = image_path
        embedding.sprite.single_image_dim.extend([100, 100])

        # save embedding_var
        projector.visualize_embeddings(summary_writer, config)
        sess.run(tf.variables_initializer([embedding_var]))
        saver = tf.train.Saver([embedding_var])
        saver.save(sess, os.path.join(basedir, 'logdir', 'model.ckpt'))
    def __init__(self):
        classify_image.maybe_download_and_extract()
        self._session = tf.Session()
        classify_image.create_graph()
        self._cv_bridge = CvBridge()

        self._sub = rospy.Subscriber('image', Image, self.callback, queue_size=1)
        self._pub = rospy.Publisher('result', String, queue_size=1)
        self.score_threshold = rospy.get_param('~score_threshold', 0.1)
        self.use_top_k = rospy.get_param('~use_top_k', 5)
Exemple #6
0
	def __init__(self):
		classify_image.maybe_download_and_extract()
		self._session = tf.Session()
		classify_image.create_graph()
		self._cv_bridge = CvBridge()
		
		self._sub = rospy.Subscriber('image', Image, self.callback, queue_size=1)
		self._pub = rospy.Publisher('result', String, queue_size=1)
		self.score_threshold = rospy.get_param('~score_threshold', 0.1)
		self.use_top_k = rospy.get_param('~use_top_k', 5)
Exemple #7
0
def extract_features(file_name):
    step = 1
    d = unpickle(
        os.path.join(os.path.expanduser('cifar-10-batches-py/'), file_name))
    data = d['data']
    imgs = np.transpose(np.reshape(data, (-1, 32, 32, 3), order='F'),
                        axes=(0, 2, 1, 3))  #order batch,x,y,color
    y = np.asarray(d['labels'], dtype='uint8')
    yn = y[0:int(len(y) / step)]
    '''#Debug codes
    y_limited = np.zeros((10,))
    for x in y[range(0,len(y),1000)]:
        y_limited[x] = y[x]'''

    FLAGS = classify_image.parseArg()
    classify_image.maybe_download_and_extract(FLAGS.model_dir)
    classify_image.create_graph(FLAGS.model_dir)
    with tf.Session() as sess:
        #softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
        representation_tensor = sess.graph.get_tensor_by_name('pool_3:0')
        #predictions = np.zeros((len(y), 1008), dtype='float32')
        #representations = np.zeros((len(y), 2048), dtype='float32')

        #Debug Codes
        transferVal = np.zeros((int(len(y) / step), 2048), dtype='float32')

        for i in range(0, len(yn)):
            start = time.time()
            #[reps, preds] = sess.run([representation_tensor, softmax_tensor], {'DecodeJpeg:0': imgs[i]})
            reps = sess.run(representation_tensor, {'DecodeJpeg:0': imgs[i]})
            #if (i % 10 == 0):
            if True:
                print("{}/{} Time for image {} ".format(
                    i + 1, len(yn),
                    time.time() - start))
            #squeze to remove the dimention with shape value of 1 ( (1,1,3) chha bhane s0queeze pacchi (3,) hun6 shape; 2nd argument can be axis = (n,) kun chai shape position lai hataune ho )
            #predictions[i] = np.squeeze(preds)
            #representations[i] = np.squeeze(reps)

            #Debug Code
            transferVal[i] = np.squeeze(reps)
        #np.savez_compressed(file_name + ".npz", predictions=predictions, representations=representations, y=y)
        np.savez_compressed(file_name + ".npz",
                            representations=transferVal,
                            y=yn)
def classification_worker(image_queue, result_queue):
    """
    A worker for classification processes.

    First, it loads TensorFlow Inception v3 CNN model using create_graph() function and initializes TensorFlow session.

    Then, it executes following steps repeatedly:
    1) It gets image filename from "process-safe" queue passed in image_queue parameter.

    2) It runs predictions on the image using predict() function.

    3) It deletes the image.

    4) The result is pushed onto "process-safe" queue passed in result_queue parameter in form of dictionary {img_id: result}.
     This queue feeds the process / worker responsible for saving the results into a file.

    If an exception is thrown, it is logged and the worker continues to process next image file.

    The process / worker exits once it gets None from the image_queue.
    :param image_queue: "process-safe" queue containing filenames of images to be classified
    :param result_queue: "process-safe" queue containing classification results to be processed by results-saving worker
    :return: None
    """
    create_graph()

    with tf.Session() as sess:
        # 'softmax:0': A tensor containing the normalized prediction across
        #   1000 labels.
        softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')

        while True:
            try:
                image = image_queue.get(timeout=1)
                if image is None:
                    break
                img_id, result = predict(image, sess, softmax_tensor)
                result_queue.put({img_id: result})
                os.remove(image)
            except Empty:
                continue
            except Exception as e:
                print('Unexpected exception in classification_worker: ' +
                      str(e),
                      flush=True)
    def __init__(self):
        rospy.on_shutdown(self.cleanup);

        # 调用API从tensorflow.org下载训练好的Inception-v3模型
        classify_image.maybe_download_and_extract()
        # 创建一个TensorFlow Session()的会话对象
        self._session = tf.Session()
        # 从保存的GraphDef文件创建一个图像,并返回一个句柄
        classify_image.create_graph()

        # 创建cv_bridge话题
        self._cv_bridge = CvBridge()
        # 创建节点的订阅者和发布者
        self._sub = rospy.Subscriber('image', Image, self.callback, queue_size=1)
        self._pub = rospy.Publisher('result', String, queue_size=1)

        # 用于识别阈值和顶层预测数的参数设置
        self.score_threshold = rospy.get_param('~score_threshold', 0.1)
        self.use_top_k = rospy.get_param('~use_top_k', 5)
Exemple #10
0
class CRL(ConsumerRebalanceListener):
    def on_partitions_revoked(self, revoked):
        return

    def on_partitions_assigned(self, assigned):
        print(assigned)
        consumer.seek(TopicPartition("Panda_Media", 0), 0)
        return

maybe_download_and_extract()

consumer.subscribe(["Panda_Media"], listener=CRL())

# Creates graph from saved GraphDef.
create_graph()

# Creates node ID --> English string lookup.
node_lookup = NodeLookup()

# read messages
for msg in consumer:
    # deserialize from json
    twete = jsonpickle.decode(msg.value)

    # for each media object in tweet
    for media in twete.entities['media']:
        # base64 jpg string to bytes
        image_data = base64.b64decode(media['data'])

        # make sure image is jpeg
session = boto3.Session(profile_name=PROFILE_NAME)
AWS_ACCESS_KEY_ID = session.get_credentials().access_key
AWS_SECRET_ACCESS_KEY = session.get_credentials().secret_key
AWS_REGION_NAME = session.region_name

MODEL_DIR = '/home/ubuntu/imagenet'


class FLAGS:
    model_dir = MODEL_DIR


classify_image.FLAGS = FLAGS

classify_image.create_graph()


class Worker(object):
    def download_image(self, image_url):
        req = urllib2.Request(image_url,
                              headers={'User-Agent': "Magic Browser"})
        response = urllib.request.urlopen(req)
        return response.read()

    def run(self):
        sqs = session.resource('sqs', 'us-west-1')
        s3 = session.resource('s3')

        request_queue = sqs.get_queue_by_name(QueueName=SQS_REQUEST_QUEUE_NAME)
        response_queue = sqs.get_queue_by_name(