<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Evan Febrianto on Medium]]></title>
        <description><![CDATA[Stories by Evan Febrianto on Medium]]></description>
        <link>https://medium.com/@van.evanfebrianto?source=rss-be039f39220------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7ussXR5fNg_eTfC7LThZUA.jpeg</url>
            <title>Stories by Evan Febrianto on Medium</title>
            <link>https://medium.com/@van.evanfebrianto?source=rss-be039f39220------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 20:56:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@van.evanfebrianto/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Personal Travel Route Optimizer: A Technical Odyssey]]></title>
            <link>https://medium.com/@van.evanfebrianto/building-a-personal-travel-route-optimizer-a-technical-odyssey-e46b5b49a1fa?source=rss-be039f39220------2</link>
            <guid isPermaLink="false">https://medium.com/p/e46b5b49a1fa</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[planner]]></category>
            <category><![CDATA[route-optimization]]></category>
            <category><![CDATA[travel]]></category>
            <dc:creator><![CDATA[Evan Febrianto]]></dc:creator>
            <pubDate>Thu, 31 Aug 2023 18:06:36 GMT</pubDate>
            <atom:updated>2023-08-31T18:06:36.705Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Travel Route Optimizer" src="https://cdn-images-1.medium.com/max/1024/1*2e54h4_0qQqZxmkhx4oTpQ.png" /></figure><h3><strong>An Exciting Prelude: The Lure of the Rising Sun</strong></h3><p>Japan has always held a special place in my heart. Ever since my elementary school days, I’ve dreamt of walking along the bustling streets of Tokyo, visiting the serene temples of Kyoto, and indulging in authentic sushi by the sea. The rich culture and natural beauty of Japan fascinated me from a young age. I voraciously consumed any books, movies, and documentaries about the country to learn more. Yet, growing up, family finances were tight which meant extensive international travel was not feasible. All I could do was admire Japan from afar and hope that one day I would get the chance to experience it for myself.</p><p>Fast forward many years later, I have finally saved up enough to make my dream journey to Japan a reality. As the date of my trip draws closer, I want to immerse myself in as much as possible within the limited time frame. In this digital age, trip planning has never been easier with countless online resources at our fingertips. It was then that I stumbled upon <a href="https://wanderlog.com/"><strong>wanderlog</strong></a>, a well-designed travel itinerary planner that had glowing reviews from other users. With its comprehensive features for arranging flights, accommodation, activities and dining, wanderlog seemed like it would take the headache out of planning. However, one small hiccup occurred — their route optimization feature came with a pricey yearly subscription that didn’t quite fit my budget. As an engineer by profession, I couldn’t help but wonder if I could recreate a similar functionality on my own. And that is what set me on an unexpected technical odyssey.</p><h3><strong>A Quest Begins: The Perfect Travel Itinerary</strong></h3><p>Being a first-time traveler to Japan, I wanted to maximize every single day without wasting time. An optimized itinerary was crucial to experience as much as possible within my limited vacation days. This is where wanderlog’s route optimization tool would have been extremely helpful. It analyzes travel times and distances between locations to arrange an efficient route, saving travelers from hours of researching routes themselves. However, at $49.99 per year it was beyond what I was willing to pay for just one trip.</p><p>That’s when the programmer in me sparked into action. I had learned algorithms during my studies and done small projects before. How difficult could it be to build a basic route optimizer tailored specifically for my Japan trip? The Google Maps Distance Matrix API provided travel durations and Google Optimization Tools supported solving routing problems. With some coding knowledge and online resources, I was confident I could put together something workable. Plus, the satisfaction of creating a custom tool myself outweighed any convenience a commercial product offered. And so began my journey to develop a personal travel route optimizer from scratch.</p><h3><strong>Tackling the Travel Salesman Problem</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M4HGlKxVr0vxiZ5n5TJgiQ.png" /><figcaption>Travel Salesman Problem: Finding the shortest possible route</figcaption></figure><p>At the core of any route optimization problem lies the famous “Traveling Salesman Problem” or TSP. In its simplest form, TSP aims to find the shortest route possible that visits each location on a list only once before returning to the starting point. Mathematicians have worked on variations of this problem for decades as an intriguing puzzle with practical applications like planning delivery routes.</p><p>For my optimizer, the locations would be all the places I wanted to visit in Japan. The goal was to determine the most efficient order to travel between them that minimized total distance or time. Of the various algorithms available to tackle TSP, Google Optimization Tools (OR-Tools) stood out for its Python library and ease of use. It provided implementations of different methods from brute force to more advanced heuristics like genetic algorithms. After evaluating the approaches, I decided the heuristics algorithm offered the right balance of performance and speed needed for an interactive user experience.</p><p>With OR-Tools incorporated, my optimize_route() function could now accept a list of destinations and return the optimal sequence. It first retrieved travel durations between each pair of locations from Google Maps. This data was fed into the TSP solver which worked behind the scenes to analyze billions of possible routes before finding the shortest one. The output was then formatted into an intuitive itinerary I could follow on my trip. It was thrilling to see the fruits of solving such a renowned computational problem!</p><pre>class RouteOptimizer:<br>    ...<br>    def optimize_route(self) -&gt; Dict[str, Any]:<br>        &quot;&quot;&quot;<br>        Optimizes the travel route using the Travelling Salesman Problem approach.<br>        :return: A dictionary containing the optimized route.<br>        &quot;&quot;&quot;<br>        distance_matrix = self.google_maps_api.get_distance_matrix(<br>            self.destinations, self.transportation_mode, self.departure_time<br>        )<br>        # Check for any origin with ZERO_RESULTS for all routes<br>        for idx, row in enumerate(distance_matrix[&quot;rows&quot;]):<br>            if all(<br>                element.get(&quot;status&quot;) == &quot;ZERO_RESULTS&quot; for element in row[&quot;elements&quot;]<br>            ):<br>                return {<br>                    &quot;error&quot;: f&quot;No route can be found from &#39;{self.destinations[idx]}&#39; to any other destinations.&quot;<br>                }<br>        data = self.create_data_model(distance_matrix)<br>        # Create the routing index manager.<br>        manager = pywrapcp.RoutingIndexManager(<br>            len(data[&quot;distance_matrix&quot;]), data[&quot;num_vehicles&quot;], data[&quot;depot&quot;]<br>        )<br><br>        # Create Routing Model.<br>        routing = pywrapcp.RoutingModel(manager)<br><br>        def distance_callback(from_index, to_index):<br>            return data[&quot;distance_matrix&quot;][manager.IndexToNode(from_index)][<br>                manager.IndexToNode(to_index)<br>            ]<br><br>        transit_callback_index = routing.RegisterTransitCallback(distance_callback)<br><br>        # Define cost of each arc.<br>        routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)<br><br>        # Setting first solution.<br>        search_parameters = pywrapcp.DefaultRoutingSearchParameters()<br>        search_parameters.first_solution_strategy = (<br>            routing_enums_pb2.FirstSolutionStrategy.AUTOMATIC<br>        )<br><br>        # Solve the problem.<br>        solution = routing.SolveWithParameters(search_parameters)<br><br>        # Get the optimized route and travel times<br>        index = routing.Start(0)<br>        route = [self.destinations[manager.IndexToNode(index)]]<br>        travel_times = []<br>        while not routing.IsEnd(index):<br>            previous_index = index<br>            index = solution.Value(routing.NextVar(index))<br>            route.append(self.destinations[manager.IndexToNode(index)])<br>            from_index = manager.IndexToNode(previous_index)<br>            to_index = manager.IndexToNode(index)<br>            if &quot;duration&quot; in distance_matrix[&quot;rows&quot;][from_index][&quot;elements&quot;][to_index]:<br>                travel_times.append(<br>                    distance_matrix[&quot;rows&quot;][from_index][&quot;elements&quot;][to_index][<br>                        &quot;duration&quot;<br>                    ][&quot;text&quot;]<br>                )<br><br>        return {&quot;route&quot;: route, &quot;travel_times&quot;: travel_times}<br>        ...</pre><h3><strong>Discovering New Horizons: Clustering for Convenience</strong></h3><p>As more details of my Japan trip took shape, a friend offered some local insider advice — group places based on proximity to visit together in a day rather than hopping around randomly. This clustering concept made total sense to compress multiple destinations within walking distances into efficient daily routes. It would allow more flexibility to spend extra time at certain locations if desired too.</p><p>Incorporating clustering into my optimizer brought two key benefits:</p><blockquote>1. I could input the number of days allotted for each city/region and it would split locations accordingly.<br>2. Closely packed clusters minimized backtracking for a relaxed pace perfect for exploring neighborhoods leisurely.</blockquote><p>When investigating clustering algorithms, K-means initially seemed suitable for partitioning based on distances. However, its rigid grouping ignored characteristics of real-world points distribution. Meanwhile, Gaussian Mixture Modeling (GMM) smartly identified the shape, spread and orientation of location clusters — insightful for traversing Tokyo’s varied districts.</p><p>My cluster_destinations_gmm() function utilized the powerful scikit-learn GMM implementation. By detecting the underlying patterns, it assigned locations to natural daily routes respecting each area’s unique identity. This user-friendly approach aligned well with experiencing different facets of huge metropolitan regions over multiple days instead of chaotic short hopping. The result was shaping up nicely!</p><pre>def cluster_destinations_gmm(self, n_clusters):<br>    &quot;&quot;&quot;<br>    Cluster the given destinations using Gaussian Mixture Model.<br>    :param n_clusters: Number of clusters to form.<br>    :return: Cluster labels for each destination.<br>    &quot;&quot;&quot;<br>    # Convert destination names to lat-lon coordinates<br>    destinations_coords = []<br>    for destination in self.destinations:<br>        lat_lng = self.google_maps_api.get_lat_lng(destination)<br>        lat_lng[&quot;lon&quot;] = lat_lng.pop(&quot;lng&quot;)<br>        destinations_coords.append(lat_lng)<br>    <br>    # Extract latitudes and longitudes from the coordinates<br>    data = np.array([[d[&quot;lat&quot;], d[&quot;lon&quot;]] for d in destinations_coords])<br>    <br>    # Create a Gaussian Mixture Model with the specified number of components (clusters)<br>    gmm = GaussianMixture(<br>        n_components=n_clusters,<br>        init_params=&quot;k-means++&quot;,<br>        covariance_type=&quot;full&quot;,<br>        reg_covar=1e-06,<br>        random_state=0,<br>    )<br>    <br>    # Fit the GMM to the data<br>    gmm.fit(data)<br>    <br>    # Predict the cluster for each destination<br>    labels = gmm.predict(data)<br>    <br>    return labelsThe Interface: Crafting an Intuitive Frontend with Streamlit</pre><p>While backend algorithms excited my inner coder, an intuitive frontend sealed the deal to make this a truly usable trip planner. This is where Streamlit brought the visions to life effortlessly. Being new to front-end frameworks, its simple Python API allowed rapid prototyping without diving into HTML/CSS nitty-gritties.</p><p>The main UI components were:</p><ul><li>Data input section to add destinations, dates, transport mode</li><li>Map display using Folium to view pinpointed locations</li><li>Clustering results visualized on the map</li><li>Optimized daily route pages with travel durations</li></ul><h4>Data Input</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*A0sQHlW1PGiqbonWzD2gPA.gif" /><figcaption>Data Input Includes Destinations, Transportation Mode, Timing, and Number of Days Travelling</figcaption></figure><p>As this is for personal use, I tried to keep the interface simple yet interactive. The user can input all the places they want to visit, along with their hotel at the top of the list. Additional options include specifying the transportation mode, preferred timing, and the number of days available for traveling. By clicking the save button, the user can visualize their destinations on a map, thanks to the power of the folium library.</p><p>Using the display_map_with_markers function, I was able to represent my destinations on the map, providing a visual aid for planning the itinerary.</p><pre>def display_map_with_markers(locations, destinations):<br>    # Create a map centered around the first location<br>    m = folium.Map(location=[locations[0][&quot;lat&quot;], locations[0][&quot;lon&quot;]])<br><br>    # Create an empty list to store the lat-lon pairs for bounds<br>    latlons = []<br><br>    for idx, (loc, destination) in enumerate(zip(locations, destinations)):<br>        folium.Marker(location=[loc[&quot;lat&quot;], loc[&quot;lon&quot;]], popup=destination).add_to(m)<br>        latlons.append((loc[&quot;lat&quot;], loc[&quot;lon&quot;]))<br><br>    # Fit the map&#39;s bounds to the provided lat-lon pairs<br>    m.fit_bounds(latlons)<br><br>    folium_static(m)</pre><h4>Clustering</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Kr75DCCmsS8C157rhOxmmw.gif" /><figcaption>Clustered Destinations</figcaption></figure><p>Once the destinations are plotted on the map, the clustering algorithm takes over. The user can specify the number of clusters they desire, and the program will group the destinations accordingly. This feature helps organize the itinerary by creating coherent groups of places to visit each day.</p><h4>Getting the Optimized Route</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PNqo0L_8NucRdw-nVZ72Pw.png" /><figcaption>An optimized route for day-1</figcaption></figure><p>To generate an optimized route, the user simply needs to select the cluster they want to explore on a particular day. The program will then calculate the shortest possible route between the destinations within that cluster, taking into account travel time and distance.</p><p>Additionally, I added a feature to display the estimated travel time between each destination. This helps the user plan their day more efficiently and make informed decisions about their itinerary. Travel time shown as a pop up when clicking a destination.</p><p>The travel time in the image below is showing 6 mins. That means it probably takes 6 mins to drive from Ginza Shopping District (destination-3) to Tsukiji Outer Market (destination-4).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fy3OA19Fh0E5G7_7IMhZGQ.png" /><figcaption>Travel time shown as a pop up when clicking a destination</figcaption></figure><p>I did a quick validation using Google Map and it shows also shows 6 mins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hh7naaoMBZR4JEcM5OpG6Q.png" /><figcaption>Travel Time Validation on Google Map</figcaption></figure><p>Overall, it was fascinating to see the places I’d dreamt of visiting for so long neatly marked and optimized on a map! But for those who is wondering how I displayed the route into the map, here’s a snippet of my code.</p><pre>def decode_polyline(polyline_str):<br>    return polyline.decode(polyline_str)<br><br><br>def display_map_with_route(locations, destinations, route=None, travel_times=None):<br>    m = folium.Map(location=[locations[0][&quot;lat&quot;], locations[0][&quot;lon&quot;]])<br>    latlons = []<br><br>    for idx, (loc, destination) in enumerate(zip(locations, destinations)):<br>        is_last_same_as_first = (idx == len(locations) - 1) and (<br>            loc[&quot;lat&quot;] == locations[0][&quot;lat&quot;] and loc[&quot;lon&quot;] == locations[0][&quot;lon&quot;]<br>        )<br>        if is_last_same_as_first:<br>            icon_label = &quot;1&quot;<br>        else:<br>            icon_label = str(idx + 1)<br><br>        popup_text = destination<br>        if travel_times and idx &gt; 0:  # Start from the second destination<br>            popup_text += f&quot;&lt;br&gt;Travel Time: {travel_times[idx - 1]}&quot;<br><br>        folium.Marker(<br>            location=[loc[&quot;lat&quot;], loc[&quot;lon&quot;]],<br>            tooltip=destination,<br>            popup=popup_text,<br>            icon=folium.Icon(icon=icon_label, prefix=&quot;fa&quot;, color=&quot;blue&quot;),<br>        ).add_to(m)<br>        latlons.append((loc[&quot;lat&quot;], loc[&quot;lon&quot;]))<br><br>    if route:<br>        folium.PolyLine(route, color=&quot;blue&quot;, weight=2.5, opacity=1).add_to(m)<br>        latlons.extend(route)<br><br>    m.fit_bounds(latlons)<br>    folium_static(m)</pre><h3>Wrapping It Up: A Rewarding Journey</h3><p>Revisiting this personal project, I’m amazed by how far it has come since that initial spark of curiosity. Each technical challenge taught me new skills and deepened my knowledge. But more importantly, this experience reshaped how I approach ambitious goals — with perseverance, research and persistently learning from mistakes.</p><p>My homemade travel assistant may not compete with sophisticated commercial offerings. However, it was tailored specifically for my Japan trip needs. Best of all, the satisfaction of following through an idea into a working product is priceless. I can now look back proudly on this hands-on process that brought my childhood dream vacation closer.</p><p>Most insightful of all, I discovered unexpected passions within. Coding exposed new perspectives and wondrous solutions to everyday problems. Who knows where this may lead, but for now I’m grateful for the opportunities and adventures it creates. So as I finish final preparations for Japan, this customized route optimizer will not only be a practical tool but a reminder of endless possibilities when curiosity meets dedication.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e46b5b49a1fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Deep Dive into Monitoring Character Consumption in Langchain for VertexAI: Ensuring Business…]]></title>
            <link>https://medium.com/@van.evanfebrianto/a-deep-dive-into-monitoring-character-consumption-in-langchain-for-vertexai-ensuring-business-d4b6363802a5?source=rss-be039f39220------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4b6363802a5</guid>
            <category><![CDATA[language-model]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[vertex-ai]]></category>
            <dc:creator><![CDATA[Evan Febrianto]]></dc:creator>
            <pubDate>Fri, 04 Aug 2023 16:48:57 GMT</pubDate>
            <atom:updated>2023-08-04T16:48:57.092Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Deep Dive into Monitoring Character Consumption in Langchain for VertexAI: Ensuring Business Efficiency</h3><p>LangChain is an extraordinary framework for the adventurous developers. It empowers applications to connect to diverse data sources and allows language models to have meaningful interactions with their surroundings. This is a unique space where components are like building blocks, and off-the-shelf chains come ready to assemble your dream projects.</p><p>But a ship is nothing without a compass, and in LangChain’s vast ocean, tracking token usage can be your guiding star. For businesses, every tiny bit counts. Knowing how much you spend is not a luxury but a necessity.</p><p>In this article, we’ll embark on a journey that will lead us to develop a custom callback to track the usage of characters or tokens, particularly focusing on the VertexAI model. But why VertexAI? It is simply because that was one of the requirements I had in building my application, and I realized how many others might benefit from this path.</p><h3>VertexAI Pricing: The Foundation of Our Quest</h3><p>Langchain’s integration with OpenAI models is smooth and seamless, providing tracking token usage right out of the box. For those who are using OpenAI models and want to leverage this feature, you can find more information <a href="https://python.langchain.com/docs/modules/model_io/models/llms/token_usage_tracking">here</a>.</p><p>Now, before jumping into the technical details, let’s take a glance at how Vertex AI pricing works. Vertex AI’s pricing mechanism is intriguing, as it charges for characters, translating text processing into a tangible cost. Specifically, it’s 0.0010 USD per 1,000 characters for PALM 2 Text Bison and 0.0005 USD for PALM 2 Chat Bison. Knowing the cost per character is the first step in controlling expenses and building a transparent, sustainable tech ecosystem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*WBL4GsO2pxwPcF_UJzKD5w.png" /><figcaption>Understanding VertexAI’s Pricing Mechanism</figcaption></figure><p>For those with an insatiable curiosity about their pricing structure, you can find more <a href="https://cloud.google.com/vertex-ai/pricing#generative_ai_models">here</a>.</p><h3>The Building Blocks: Functions that Hold the Key</h3><p>Building a system that works is not only about putting pieces together but understanding every piece’s role. Let’s build the foundation first.</p><h4>Dictionary Creation</h4><p>To accommodate possible future changes in pricing for input and output, let’s construct a dictionary to capture these details.</p><pre>MODEL_COST_PER_1K_CHARACTERS = {<br>    # Text Bison input<br>    &quot;text-bison&quot;: 0.0010,<br>    &quot;text-bison@001&quot;: 0.0010,<br>    # Text Bison output<br>    &quot;text-bison-output&quot;: 0.0010,<br>    &quot;text-bison@001-output&quot;: 0.0010,<br>    # Chat Bison input<br>    &quot;chat-bison&quot;: 0.0005,<br>    &quot;chat-bison@001&quot;: 0.0005,<br>    # Chat Bison output<br>    &quot;chat-bison-output&quot;: 0.0005,<br>    &quot;chat-bison@001-output&quot;: 0.0005,<br>    # Code Generation input<br>    &quot;code-bison@001&quot;: 0.0005,<br>    # Code Generation output<br>    &quot;code-bison-output&quot;: 0.0005,<br>    # Code Chat input<br>    &quot;codechat-bison@001&quot;: 0.0005,<br>    # Code Chat output<br>    &quot;codechat-bison@001-output&quot;: 0.0005,<br>    # Code Completion input<br>    &quot;code-gecko@001&quot;: 0.0005,<br>    # Code Completion output<br>    &quot;code-gecko@001-output&quot;: 0.0005,<br>    # Text Embeddings input<br>    &quot;textembedding-gecko@001&quot;: 0.0001,<br>    # Text Embeddings output<br>    &quot;textembedding-gecko@001-output&quot;: 0,<br>}</pre><h4>Matching Models to Dictionary</h4><p>Matching the models to our dictionary requires the creation of a function that can translate our needs.</p><pre>def standardize_model_name(<br>    model_name: str,<br>    is_output: bool = False,<br>) -&gt; str:<br>    &quot;&quot;&quot;<br>    Standardize the model name to a format that can be used to distinguish the output.<br><br>    Args:<br>        model_name: Model name to standardize.<br>        is_output: Whether the model is used for output or not.<br>            Defaults to False.<br><br>    Returns:<br>        Standardized model name.<br><br>    &quot;&quot;&quot;<br>    model_name = model_name.lower()<br>    if is_output:<br>        return model_name + &quot;-output&quot;<br>    else:<br>        return model_name</pre><h4>Calculating Cost with Care</h4><p>Next, we need to meticulously calculate the cost based on our dictionary. Accuracy is paramount here.</p><pre>def get_vertexai_characters_cost_for_model(<br>    model_name: str, num_characters: int, is_output: bool = False<br>) -&gt; float:<br>    &quot;&quot;&quot;<br>    Get the cost in USD for a given model and number of characters.<br><br>    Args:<br>        model_name: Name of the model<br>        num_characters: Number of characters.<br>        is_output: Whether the model is used for output or not.<br>            Defaults to False.<br><br>    Returns:<br>        Cost in USD.<br>    &quot;&quot;&quot;<br>    model_name = standardize_model_name(model_name, is_output=is_output)<br>    if model_name not in MODEL_COST_PER_1K_CHARACTERS:<br>        raise ValueError(<br>            f&quot;Unknown model: {model_name}. Please provide a valid OpenAI model name.&quot;<br>            &quot;Known models are: &quot; + &quot;, &quot;.join(MODEL_COST_PER_1K_CHARACTERS.keys())<br>        )<br>    return MODEL_COST_PER_1K_CHARACTERS[model_name] * (num_characters / 1000)</pre><h4>Counting Characters, The VertexAI Way</h4><p>The uniqueness of VertexAI&#39;s character counting, where white spaces are excluded and characters are counted by UTF-8 code points, necessitates a special approach. You can check this in their pricing model.</p><blockquote>“Characters are counted by UTF-8 code points and white space is excluded from the count.”</blockquote><p>We’ll need to build a function to calculate characters accordingly.</p><pre>def count_characters(s: str) -&gt; int:<br>    &quot;&quot;&quot;<br>    Count the number of non-whitespace characters in a string by UTF-8.<br><br>    Args:<br>        s (str): The input string.<br><br>    Returns:<br>        int: The number of non-whitespace characters in the string.<br>    &quot;&quot;&quot;<br>    return sum(1 for c in s if not c.isspace())</pre><h3>Crafting the Custom Callback: Where Magic Happens</h3><p>With our building blocks in place, now comes the enchanting part of building a custom callback.</p><pre>from typing import Any, Dict, List<br>from langchain.schema import LLMResult<br>from langchain.callbacks.base import BaseCallbackHandler<br><br>class VertexAIHandler(BaseCallbackHandler):<br>    &quot;&quot;&quot;Callback Handler that tracks VertexAI info.&quot;&quot;&quot;<br>    def __init__(self, llm_instance):<br>        super().__init__()<br>        self.llm_instance = llm_instance<br>        self.total_characters: int = 0<br>        self.input_characters: int = 0<br>        self.output_characters: int = 0<br>        self.successful_requests: int = 0<br>        self.total_cost: float = 0.0<br><br>    def __repr__(self) -&gt; str:<br>        return (<br>            f&quot;Total Characters Used: {self.total_characters}\n&quot;<br>            f&quot;\tInput Characters: {self.input_characters}\n&quot;<br>            f&quot;\tOutput Characters: {self.output_characters}\n&quot;<br>            f&quot;Successful Requests: {self.successful_requests}\n&quot;<br>            f&quot;Total Cost (USD): ${self.total_cost}&quot;<br>        )<br><br>    def on_llm_start(<br>        self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any<br>    ) -&gt; None:<br>        &quot;&quot;&quot;Print out the prompts.&quot;&quot;&quot;<br>        if len(prompts) &gt; 1:<br>            raise ValueError(f&quot;Multiple prompts not supported. Supplied prompts: {prompts}&quot;)<br>        self.prompt = prompts[0]<br><br>    def on_llm_end(self, response: LLMResult, **kwargs: Any) -&gt; None:<br>        &quot;&quot;&quot;Count characters.&quot;&quot;&quot;<br>        self.successful_requests += 1<br>        input_characters = count_characters(self.prompt)<br>        output_characters = count_characters(response.flatten()[0].generations[0][0].text)<br>        model_name = standardize_model_name(self.llm_instance.model_name)<br>        if model_name in MODEL_COST_PER_1K_CHARACTERS:<br>            output_cost = get_vertexai_characters_cost_for_model(<br>                model_name, output_characters, is_output=True<br>            )<br>            input_cost = get_vertexai_characters_cost_for_model(model_name, input_characters)<br>            self.total_cost += input_cost + output_cost<br>        self.input_characters += input_characters<br>        self.output_characters += output_characters<br>        self.total_characters = self.input_characters + self.output_characters</pre><p>This custom callback is like a potion brewer that keeps track of essential parameters like the number of characters and the total cost. It diligently calculates and updates the properties, ensuring you are never lost in the sea of characters and costs.</p><h3>Putting Our Creation to Test</h3><p>A ship untested in rough waters is a risk. Let’s ensure our creation can weather the storm.</p><h4>Instantiating and Querying</h4><p>We bring our llm object to life and put our callback into action with a simple query.</p><pre>from langchain.llms import VertexAI<br><br>llm = VertexAI(temperature=0, model_name=&quot;text-bison&quot;, <br>               max_output_tokens=1024, top_k=40, top_p=0.95)<br>handler = VertexAIHandler(llm)<br>llm.callbacks = [handler]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*_TzdY8Q6SJvhgsQSnNKAIw.png" /><figcaption>Making a simple query</figcaption></figure><p>And we get our response: The square root of 9 is 3. So, 2 * 3 = 6.</p><p>Now, let’s ensure our callback is navigating properly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/233/1*dJ2vnIj83QJKEZKFRHZ_mA.png" /><figcaption>Handler is showing the tracked characters and cost</figcaption></figure><p>Until this point, we have successfully implemented our callback to track input and output characters and estimate the cost.</p><h3>Validating the Calculation</h3><p>Nothing like a successful validation to put our minds at ease. So, we are validating through a single API request to the GCP endpoint. When calling the API, it’s important to make sure that we use the same model ID and parameter in our testing. For the temperature, we need to make sure that it’s set to 0 so we can have the same result as in our notebook.</p><pre>API_ENDPOINT=&quot;us-central1-aiplatform.googleapis.com&quot;<br>PROJECT_ID=[YOUR-PROJECT-ID]<br>MODEL_ID=&quot;text-bison&quot;<br><br>curl \<br>-X POST \<br>-H &quot;Authorization: Bearer $(gcloud auth print-access-token)&quot; \<br>-H &quot;Content-Type: application/json&quot; \<br>&quot;https://${API_ENDPOINT}/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:predict&quot; -d \<br>$&#39;{<br>    &quot;instances&quot;: [<br>        {<br>            &quot;content&quot;: &quot;What is the square root of 9 and then multiply it by 2?&quot;<br>        }<br>    ],<br>    &quot;parameters&quot;: {<br>        &quot;temperature&quot;: 0,<br>        &quot;maxOutputTokens&quot;: 1024,<br>        &quot;topP&quot;: 0.95,<br>        &quot;topK&quot;: 40<br>    }<br>}&#39;</pre><p>Here’s the response we got:</p><pre>{<br>  &quot;predictions&quot;: [<br>    {<br>      &quot;safetyAttributes&quot;: {<br>        &quot;categories&quot;: [],<br>        &quot;blocked&quot;: false,<br>        &quot;scores&quot;: []<br>      },<br>      &quot;content&quot;: &quot;The square root of 9 is 3. So, 2 * 3 = 6.&quot;,<br>      &quot;citationMetadata&quot;: {<br>        &quot;citations&quot;: []<br>      }<br>    }<br>  ],<br>  &quot;metadata&quot;: {<br>    &quot;tokenMetadata&quot;: {<br>      &quot;inputTokenCount&quot;: {<br>        &quot;totalBillableCharacters&quot;: 43,<br>        &quot;totalTokens&quot;: 16<br>      },<br>      &quot;outputTokenCount&quot;: {<br>        &quot;totalBillableCharacters&quot;: 29,<br>        &quot;totalTokens&quot;: 21<br>      }<br>    }<br>  }<br>}</pre><p>And there we have it, validation success! Our input and output characters shown in the callback are 43 and 29 respectively. These numbers match with totalBillableCharacters and totalBillableCharacters .</p><h3>Running Multiple Requests: Stress Testing the Waters</h3><p>It’s time to see if our creation can handle the waves of multiple requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*5FyULvTu50l6IqKJPZSOMw.png" /><figcaption><em>Results of calling multiple requests</em></figcaption></figure><h3>Checking the Total Cost: The Final Verdict</h3><p>Now, we simply check our callback by calling the handler variable again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*lZkHv8KqVnYVjiieVTLPYA.png" /><figcaption><em>Final Total Cost after Multiple Requests.</em></figcaption></figure><p>Our journey ends with a successful exploration, with three successful requests and a cost that’s a mere drop in the ocean.</p><h3>Conclusion: A Voyage Well Travelled</h3><p>This voyage into the heart of LangChain’s seas has brought forth a versatile callback, capable of aligning with various use cases like document summarization, classification, chatbots, and more. The journey has been intricate but enlightening.</p><p>Through the winds and waves, we have discovered how to extend LangChain’s capabilities to better suit our needs, specifically for VertexAI. We have not only unlocked doors for ourselves but opened gates for others to tread the same path.</p><p>In the grand seascape of LangChain, we have not only found our way but charted a course for others to follow. It’s not merely a code; it’s a map. And as I sit on the deck, gazing at the stars, I know that another adventure awaits. Perhaps, next time, you’ll join me on another exciting journey through the captivating world of language models and applications.</p><p>Until then, happy sailing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4b6363802a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Train Custom Object Detection Models using RetinaNet]]></title>
            <link>https://medium.com/@van.evanfebrianto/how-to-train-custom-object-detection-models-using-retinanet-aeed72f5d701?source=rss-be039f39220------2</link>
            <guid isPermaLink="false">https://medium.com/p/aeed72f5d701</guid>
            <category><![CDATA[retinanet]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[Evan Febrianto]]></dc:creator>
            <pubDate>Thu, 25 Jun 2020 11:52:39 GMT</pubDate>
            <atom:updated>2020-06-25T11:52:39.979Z</atom:updated>
            <content:encoded><![CDATA[<p>Back to 2018 when I got my first job to create a custom model for object detection. I was completely lost because I was a newbie haha. After the model created I forgot to document it. The spark of documenting my work came a few weeks ago and I started to think about it. So, here I am. In my first medium story, I will write how to train custom object detection models using RetinaNet.</p><h3>Installation</h3><ol><li>Clone RetinaNet repository <a href="https://github.com/fizyr/keras-retinanet">https://github.com/fizyr/keras-retinanet</a></li><li>Ensure NumPy is installed using pip install numpy --user</li><li>Ensure TensorFlow has been installed before executing the next step</li><li>Navigate to the RetinaNet repository and execute pip install . --user</li><li>Run python setup.py build_ext --inplace to compile Cython code first</li></ol><h3>Running Example</h3><ol><li>Download RetinaNet model available from <a href="https://github.com/fizyr/keras-retinanet/releases">https://github.com/fizyr/keras-retinanet/releases</a> , choose resnet50_coco_best_v2.1.0.h5</li><li>Place the model inside the snapshots folder in the repository</li><li>Navigate to examples folder and execute python ResNet50RetinaNet.py</li><li>If you can see the result, it means that you have successfully installed RetinaNet on your PC. The result will look like the image below.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zv-aJeYBoc88yl0HQu96DA.png" /><figcaption>object detection result from examples/ResNet50RetinaNet.py</figcaption></figure><h3>Train Custom Dataset</h3><ul><li><a href="#3a51">Step 1: Prepare your own dataset</a></li><li><a href="#64db">Step 2: Annotation</a></li><li><a href="#43ce">Step 3: Define classes</a></li><li><a href="#9426">Step 4: Train your model</a></li></ul><h4>Prepare Your Own Dataset</h4><p>First thing first, you need to define what object you want to detect. Dataset takes one of the most important parts in creating deep learning models, as people say, “<em>garbage in, garbage out</em>”. These days, lots of datasets are available on the internet, one of my favorites is <a href="https://www.kaggle.com/datasets">https://www.kaggle.com/datasets</a></p><p>In this case, I’ll use it to detect 8 different tools with a controlled environment. Used as my dataset, pictures were taken by myself with a Logitech webcam mounted with my PC. I prepared around 2,165 images for training and 58 images for testing.</p><p>Note that you need to place all of the images in one folder.</p><h4>Annotation</h4><p>The next step is to annotate your dataset. I used labelImg from <a href="https://github.com/tzutalin/labelImg">https://github.com/tzutalin/labelImg</a></p><p>It is a graphical image annotation tool that is written in Python and uses Qt for its graphical interface. Annotations are saved as XML files in PASCAL VOC format, the format used by <a href="http://www.image-net.org/">ImageNet</a>. Besides, it also supports YOLO format. However, I used PASCAL VOC format since it is supported by RetinaNet. If you’re not familiar with this annotation tool, you can check the video tutorial <a href="https://youtu.be/p0nR2YsCY_U">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IXiGusQ1w-RtPqF_VX6uTg.png" /><figcaption>labelImg for annotation</figcaption></figure><p>LabelImg is not the only annotation tools available from the internet, you can also try other annotation tools as long as the output is XML files in PASCAL VOC format.</p><h4>Define Classes</h4><p>Modifying the original code is required in this step. You need to define the class inside keras_retinanet/preprocessing/pascal_voc.py</p><p>Original code will define classes as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b7b4727cf9f2741c3f60f874bfd11f1/href">https://medium.com/media/0b7b4727cf9f2741c3f60f874bfd11f1/href</a></iframe><p>Once you found the code, you have to change the voc_classes accordingly. In my case, I will put it this way.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54a02a80eae859361d9cb546d4a6f509/href">https://medium.com/media/54a02a80eae859361d9cb546d4a6f509/href</a></iframe><p>Please note that the name should remain the same, which is pascal_voc.py</p><h4>Train Your Model</h4><p>Split your dataset to be training set and test set following this directory format. All of the XML files need to be placed inside the <em>Annotations</em> folder while the <em>JPEGImages </em>folder will only contain dataset images. Inside the <em>ImageSets</em> folder, there is a subfolder named <em>Main </em>which contains train.txt and test.txt</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e316ad491caef2016aa29a1c03a7300/href">https://medium.com/media/9e316ad491caef2016aa29a1c03a7300/href</a></iframe><p>train.txt and test.txt contain list of the dataset file name without the extension. Below is the code to split the dataset and produce both train.txt and test.txt with 80:20 ratio.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c3fe1c3a0f9cd9c0bf53de219021f7b/href">https://medium.com/media/7c3fe1c3a0f9cd9c0bf53de219021f7b/href</a></iframe><p>Now, we are ready to train the model. Navigate to parent keras-retinanet directory and execute this command below</p><pre>python keras_retinanet/bin/train.py<br>--imagenet-weights<br>--gpu &lt;gpuID, default=0&gt;<br>--epoch &lt;put your total epoch&gt;<br>--batch-size &lt;put your batch-size&gt;<br>--steps &lt;put your total steps&gt;<br>--snapshot-path &lt;path to store snapshots of models&gt;<br>--tensorboard-dir &lt;log directory for Tensorboard output&gt;<br>pascal &lt;path to dataset directory&gt;</pre><p>Utilizing GPU can speed up the training process. Therefore, I highly recommend you to use GPU to train the model. RetinaNet takes lots of VRAM so that the default batch-size is 1. Based on my experience, 1 batch-size for RetinaNet with RestNet50 backbone takes 3,400 MiB memory.</p><p>Tensorboard is useful to track the training process. Below is my training record for the epoch classification loss and mAP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*TLq_qksJQft-U7Fj2edouQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*YFrbh2RDPpPu2Twh17oQzA.png" /><figcaption>epoch_classification_loss (left) and epoch_mAP (right)</figcaption></figure><p>You may need to convert your model from the training model to the inference model by running</p><pre>python keras_retinanet/bin/convert_model.py &lt;/path/to/training/model.h5&gt; &lt;/path/to/save/inference/model.h5&gt;</pre><p>You may need to modify the example script to test your model. This is my result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ErQxmdwkzHuKlbdWhOpdaQ.gif" /><figcaption>inference result of the model</figcaption></figure><p>I would like to improve the performance in the future by tuning some parameters including anchor optimization.</p><h3>Resources:</h3><ul><li><a href="https://github.com/fizyr/keras-retinanet">fizyr/keras-retinanet</a></li><li><a href="https://github.com/tzutalin/labelImg">GitHub - tzutalin/labelImg: 🖍️ LabelImg is a graphical image annotation tool and label object bounding boxes in images</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aeed72f5d701" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>